【问题标题】:Parsing JSON with jQuery turns array into hash使用 jQuery 解析 JSON 将数组转换为哈希
【发布时间】:2014-09-15 21:10:48
【问题描述】:

我有一个这样的字符串:

string = '{ "key": [
  { "foo": "bar" }
] }';

这个字符串通过做转换成一个JSON对象

json = $.parseJSON(string);

然后它看起来像这样:

{ "key":
  { "0":
    { "foo": "bar" }
  }
}

所以看起来数组被转换成哈希了。

期望的结果是:

{ "key": [
  { "foo": "bar" }
] }

实现这一目标的最佳方法是什么?背景:我将 JSON 数据发布到 URL,但需要数组保持完整,以便收件人可以相应地解析它。

更新

这是我在 Chrome 37.0.2062.120 和 jQuery 1.11.1 的控制台中看到的:

它看起来像一个数组,但实际上只是另一个键为“0”的散列。还是我搞错了?

更新 2

将字符串转换为 JSON 更新后,我将其发布到 url:

$.ajax({
    url: 'http://test.com',
    data: json,
    dataType: 'jsonp',
    type: 'post'
  })

它到达的地方

{ "key":
  { "0":
    { "foo": "bar" }
  }
}

【问题讨论】:

  • 您的字符串不是有效的 Json。我得到“Uncaught SyntaxError: Unexpected token :”。如果您以 '{"key": [{ "foo": "bar" }]}' 之类的有效 Json 字符串开头,您将得到预期的结果。
  • 抱歉,这只是一个错字。更新了我的问题。
  • 抱歉,我仍然可以重现该行为。当我解析那个字符串时,我得到了完全想要的输出。你确定这不仅仅是你如何显示结果的问题吗?
  • 是的,你在哪里“看到”这个输出?
  • 您的屏幕截图显示它确实是一个数组,您只是被按键弄糊涂了。尝试做JSON.stringify($.parseJSON(string)) 请记住,Array 扩展了 Objects,因此它们看起来也像对象,并具有其他属性,例如长度、forEach、push...

标签: javascript jquery arrays json


【解决方案1】:

发送AJAX时,可以自己编码JSON

对于 JSONP,使用

var json = '{ "key": [  { "foo": "bar" }] }';
var jsObj = $.parseJSON(json);
$.ajax({
    url: 'http://test.com',
    // You have to convert your JS object into a JSON string on your own
    // jQuery will convert them into form encoded values if you leave it as an object
    // But you want to send your whole JSON as one of the keys, 
    // so do use an object around your json to specify the the name of the
    // key value pair containing the JSON
    data: {myKey: JSON.stringify(jsObj)},
    dataType: 'jsonp',
    // The answer at http://stackoverflow.com/questions/6410810/rails-not-decoding-json-from-jquery-correctly-array-becoming-a-hash-with-intege
   // suggests you may need this for Rails to understand it
    contentType: 'application/json'
    // type can't be post for JSONP
    // type: 'post'
  })

对于 POST,使用

$.ajax({
    url: '/my/url/',
    // For POST, you can post the entire string, converting it on your own
    data: JSON.stringify(jsObj),
    type: 'POST'
  })

【讨论】:

    【解决方案2】:

    在 Javascript 中,一切都是键和值的散列,这就是为什么您会看到以索引作为散列键和值作为对象的数组的散列。

    这对我有用。

    var jsonStr = '{ "key": [{ "foo": "bar" }] }';
    
    var obj = $.parseJSON(jsonStr);
    if (obj.key instanceof Array) {
    alert('value is Array! and its length is > '+obj.key.length);
    } else {
    alert('Not an array');
    }
    

    http://jsbin.com/rifinu/1/

    【讨论】:

      【解决方案3】:

      一方面,所有东西都需要像这样用大括号包裹起来

      string = '{"key": [
        { "foo": "bar" }
      ]}';
      

      这会让jQuery用jQuery.parseJSON(string)解析它

      它被包裹在一个数组中,这就是你得到一个数组的原因。我在这里猜测,但这是你真正想要的:

      string = '{"key": 
        { "foo": "bar" }
      }';
      

      这样您就可以通过点击array['key']['foo'] 来访问“栏”?如果是这样,请删除这些括号。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-10-27
        • 2019-05-23
        • 2012-10-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多