I have a hidden input field like
<input type="hidden" name="product-data" value="{Product: 'Premium', Code: 'ER412', SalesCode: 'SC415', Description: 'Premium Product Details'}" />
on click of a button I am trying to convert this value into JSON object but getting error. Here is my js code
$('.icon-edit').live('click', function(){
var data = $(this).parent().siblings('input').val();
data = jQuery.parseJSON(data); // <--- Here I am getting error
//do something with data
});
Error:
SyntaxError: JSON.parse: expected property name or '}'
I have a hidden input field like
<input type="hidden" name="product-data" value="{Product: 'Premium', Code: 'ER412', SalesCode: 'SC415', Description: 'Premium Product Details'}" />
on click of a button I am trying to convert this value into JSON object but getting error. Here is my js code
$('.icon-edit').live('click', function(){
var data = $(this).parent().siblings('input').val();
data = jQuery.parseJSON(data); // <--- Here I am getting error
//do something with data
});
Error:
SyntaxError: JSON.parse: expected property name or '}'
Share
Improve this question
edited Oct 26, 2012 at 8:18
moribvndvs
42.5k12 gold badges138 silver badges152 bronze badges
asked Oct 26, 2012 at 8:16
coure2011coure2011
42.6k87 gold badges225 silver badges361 bronze badges
1
-
parseJSON
parses JSON. Are you trying to do the opposite? – Blender Commented Oct 26, 2012 at 8:18
1 Answer
Reset to default 4JSON property names are strings, and JSON strings are delimited by "
characters.
Your property names are identifiers, and where you have string values, you have delimited them with '
. This is fine for a JavaScript object literal, but not for JSON.
<input
type="hidden"
name="product-data"
value="{"Product": "Premium", "Code": "ER412", "SalesCode": "SC415", "Description": "Premium Product Details"}"
/>
(You could also delimit the HTML attribute value with '
and use literal "
s inside it)