解析包含2个数组的JSON

时间:2018-01-04 13:27:22

标签: javascript arrays json parsing

我的ajax调用会返回如下数据:

{"result":[{"stats":{"min":{"caller_id.name":"Person1"},"count":"5"},"groupby_fields":[{"field":"caller_id","value":"ce4ddid73hes6e806d7070e21f961987"}]},{"stats":{"min":{"caller_id.name":"Person2"},"count":"2"},"groupby_fields":[{"field":"caller_id","value":"36ffbcfedbf9ba406d7070e21f96199c"}]},{"stats":{"min":{"caller_id.name":"Person3"},"count":"1"},"groupby_fields":[{"field":"caller_id","value":"714dd540dbefbe806d7070e21f96196a"}]},{"stats":{"min":{"caller_id.name":"Person4"},"count":"1"},"groupby_fields":[{"field":"caller_id","value":"ce4dd540dbefbe806d7070e21f961987"}]}]}

然后我使用var json = $.parseJSON(data);并获取此信息(来自控制台日志):

{result: Array(4)}
result:Array(4)
0:groupby_fields:Array(1)
0:{field: "caller_id", value: "ce4ddid73hes6e806d7070e21f961987"}
length:1
__proto__:Array(0)
stats:count:"5"
min:{caller_id.name: "Person1"}
__proto__:Object
__proto__:Object
1:{stats: {…}, groupby_fields: Array(1)}
2:{stats: {…}, groupby_fields: Array(1)}
3:{stats: {…}, groupby_fields: Array(1)}
length:4
__proto__:Array(0)
__proto__:Object

在之前的调用中,我遍历数据以使用类似的东西来拉取值

var callerName = json[i]["caller_id.name"];  

我不确定在这种情况下如何做到这一点。

我尝试了上面一行的一些变化,但没有取得任何成功。

我需要获得caller_idcountcaller_id.name

3 个答案:

答案 0 :(得分:0)

这应该很难。假设[...] from project import app as application [...] ImportError: No module named project 的值为i,则可以通过以下方式获取相应的值:



0




我希望您可以使用循环遍历let data = { "result": [{ "stats": { "min": { "caller_id.name": "Person1" }, "count": "5" }, "groupby_fields": [{ "field": "caller_id", "value": "ce4ddid73hes6e806d7070e21f961987" }] }, { "stats": { "min": { "caller_id.name": "Person2" }, "count": "2" }, "groupby_fields": [{ "field": "caller_id", "value": "36ffbcfedbf9ba406d7070e21f96199c" }] }, { "stats": { "min": { "caller_id.name": "Person3" }, "count": "1" }, "groupby_fields": [{ "field": "caller_id", "value": "714dd540dbefbe806d7070e21f96196a" }] }, { "stats": { "min": { "caller_id.name": "Person4" }, "count": "1" }, "groupby_fields": [{ "field": "caller_id", "value": "ce4dd540dbefbe806d7070e21f961987" }] }] }; let i = 0; console.log(data.result[i].stats.min['caller_id.name']); console.log(data.result[i].stats.count); console.log(data.result[i].groupby_fields[0].value);,并为表达式提供data.result的特定值。

答案 1 :(得分:0)

获取' caller_id.name'关键用途

var json = {"result":[{"stats":{"min":{"caller_id.name":"Person1"},"count":"5"},"groupby_fields":[{"field":"caller_id","value":"ce4ddid73hes6e806d7070e21f961987"}]},{"stats":{"min":{"caller_id.name":"Person2"},"count":"2"},"groupby_fields":[{"field":"caller_id","value":"36ffbcfedbf9ba406d7070e21f96199c"}]},{"stats":{"min":{"caller_id.name":"Person3"},"count":"1"},"groupby_fields":[{"field":"caller_id","value":"714dd540dbefbe806d7070e21f96196a"}]},{"stats":{"min":{"caller_id.name":"Person4"},"count":"1"},"groupby_fields":[{"field":"caller_id","value":"ce4dd540dbefbe806d7070e21f961987"}]}]}

//json['result'][0]["stats"]["min"]['caller_id.name']

for (x in json['result']){ console.log(r['result'][x]["stats"]["min"]['caller_id.name'])}

结果:

"Person1"
"Person2"
"Person3"
"Person4"

答案 2 :(得分:0)

JSON中有两种类型的结构:数组[value1, value2]和对象{"key1":"value1", "key2":"value2"}

在Javascript中,您可以访问:

  • 数组元素来自array[index]
  • 通过object.propertyobject['property']
  • 对象属性。如果属性名称包含特殊字符,则后者是必需的。

你可以链接这些,所以例如你可以写:

  • json.result[0].stats.min['caller_id.name']
  • json.result[0].stats.count
  • json.result[0].groupby_fields[0].field
  • json.result[0].groupby_fields[0].value

等等。