最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - SyntaxError: JSON.parse: expected property name or '}' from input value - Stack Overflow

programmeradmin3浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 4

JSON 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="{&quot;Product&quot;: &quot;Premium&quot;, &quot;Code&quot;: &quot;ER412&quot;, &quot;SalesCode&quot;: &quot;SC415&quot;, &quot;Description&quot;: &quot;Premium Product Details&quot;}" 
/>

(You could also delimit the HTML attribute value with ' and use literal "s inside it)

发布评论

评论列表(0)

  1. 暂无评论