【问题标题】:JSON Strinfy error in chrome browserchrome浏览器中的JSON Stringify错误
【发布时间】:2015-08-23 18:04:23
【问题描述】:

这是我的 json 数据

var jsonData = {
                      'user_id' : userId,
                      'app_id' : '1',
                      'entry_channel' : 'web',
                      'category' : img_category,
                      'title' : postTitle,
                      'post_content' : postContent,
                      'post_type': 'picture',
                      'for_socialevent' : { [eventId] : 'id'},
                };

因为var eventId 是一个整数值。

现在这是我对上述 json 数据进行字符串化时在 chrome 浏览器中得到的响应

{"user_id":"20","app_id":"1","entry_channel":"web","category":"socialgroup","title":"yewrtgfauiewrgflasdg","post_content":"sdfgjsdafkas;fsafsafsa","post_type":"picture","for_socialgroup":{}}

在 Firefox 中得到我需要的正确响应的地方

{"user_id":"20","app_id":"1","entry_channel":"web","category":"socialgroup","title":"sada","post_content":"dsadsa","post_type":"picture","for_socialgroup":{"20725":"id"}}

任何人都可以帮助我犯了什么错误或应该做些什么才能在 Chrome 中获得正确的结果。

【问题讨论】:

  • 你的 JSON 无效,例如 'for_socialevent' : { [eventId] : 'id'}, ,->'for_socialevent' : { 'eventId': 'id'},你应该 valid 你的 JSON
  • @InTry 不是 JSON,它是一个被字符串化为 json 的 javascript 对象。您像许多人一样混淆了这两个术语/概念。将javascript对象放入jsonlint没有好处
  • 那不行,它将使用字符串键“eventId”。您需要手动构造该对象。也就是说,它是倒退的,id 是它的本质,而不是数字的属性。

标签: jquery json google-chrome


【解决方案1】:

您不能按照您的方式动态创建对象键:

 { [eventId] : 'id'},

您需要将该属性单独添加到已创建的对象中

var jsonData = {
    'user_id': userId,
        'app_id': '1',
        'entry_channel': 'web',
        'category': img_category,
        'title': postTitle,
        'post_content': postContent,
        'post_type': 'picture',
        'socialevent': {}
};

jsonData.socialevent[eventId] = id;

正如@Dave Newton 指出的那样,这可能首先在您的初始代码中倒退,并且可能应该是:

var jsonData = {
    'user_id': userId,
        'app_id': '1',
        'entry_channel': 'web',
        'category': img_category,
        'title': postTitle,
        'post_content': postContent,
        'post_type': 'picture',
        'socialevent': {'id': eventId}
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-21
    • 2014-09-24
    • 2015-03-05
    相关资源
    最近更新 更多