简体   繁体   English

JSON返回多列,但所有值均作为单个字符串。 如何从数组中获取匹配的键值?

[英]JSON returning multiple columns, but all values as single string. How do I get matching key-value from array?

Here's my problem, I make an ajax call, get a response: 这是我的问题,我打电话给ajax,得到响应:

$.getJSON('fpCustom.cfc?method=getSysCounts',function(data){buildChart(data);});

I get a JSON reponse. 我收到JSON响应。 Raw result: 原始结果:

{"COLUMNS":["ABC","DEF","GHI"],"DATA":[[11,27,4]]}"

When I ask for COLUMN[0] , I get correct value: 'ABC' , but when I ask for DATA[0] , I get the whole DATA string: 11,27,4 . 当我要求COLUMN[0] ,我得到正确的值: 'ABC' ,但是当我要求DATA[0] ,我得到了整个DATA字符串: 11,27,4 I think it probably has to do with the double square bracket, but don't know how to fix that. 我认为这可能与双方括号有关,但不知道如何解决。

How do I get DATA[0] , which should be 11 ? 如何获取DATA[0] ,应该为11

For the JSON: 对于JSON:

{"COLUMNS":["ABC","DEF","GHI"],"DATA":[[11,27,4]]}"

The property DATA is an array of arrays. 属性DATA是一个数组数组。

Consider it like this: DATA = [a, b, c] , where a , b and c are variables. 这样考虑: DATA = [a, b, c] ,其中abc是变量。 The thing is that your a is another array, just as DATA is. 事实是,您的a就像DATA一样,是另一个数组。

This way DATA[0] , the first element of the DATA array, is an array. 这样, DATA[0]DATA数组的第一个元素)就是一个数组。

How do I get DATA[0] , which should be 11 ? 如何获取DATA[0] ,应该为11

The value you want is in: DATA[0][0] : 您想要的值位于: DATA[0][0]

Because: 因为:

DATA[0] -> [11,27,4] DATA[0] -> [11,27,4]

Then: 然后:

DATA[0][0] -> 11 DATA[0][0] -> 11
DATA[0][1] -> 27 DATA[0][1] -> 27
DATA[0][2] -> 4 DATA[0][2] -> 4

{"COLUMNS":["ABC","DEF","GHI"],"DATA":[[11,27,4]]}"

In COLUMNS is single dimentional array and DATA is two dimentional array so you have to access value of DATA[i][j] 在COLUMNS中,是一个三维数组,而DATA是两个三维数组,因此您必须访问DATA [i] [j]的值

check example 检查例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从具有多个键的 json 对象收集数据并将所有值推送到单个键:值数组 - how to collect data from json objects with multiple keys and push all values to single key:value array 如何从 JavaScript 对象数组中获取键值对? - How do I get key-value pair from array of JavaScript objects? 如何在一个键下对 JSON 数组中的所有对象的键下存在的值进行分组? - How do I group values present under a key in all objects in an array of JSON, under a single key? 如何删除具有重复键值的JSON表单JSON数组 - How do I remove JSON form JSON array with repeated key-value 从键值字符串中获取值 - Get value from key-value string 如何在单键值对数组中获取嵌套的 json 对象值 - How can i get nested json objects values in single key value pair array 如何将具有键值对的Json字符串转换为仅包含JavaScript中值的字符串 - How to convert Json string with key-value pair into string with only values in javascript 如何将字符串转换为键值json /数组? - How do I convert a string to a key value json / array? Lodash:在json中获取具有多个键值匹配的新数组 - Lodash:Getting new array with multiple key-value matches in json 我如何…通过多个cookie的JSON数组循环获取键值对? - How do I for…in loop though this JSON array of multiple cookies to get the key value pairs?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM