简体   繁体   English

使用javascript或jQuery进行JSON解析

[英]JSON parsing using javascript or jquery

I have the following valid JSON 我有以下有效的JSON

[
    {
        "series": [
            {
                "name": "Comp",
                "data": [
                    753,
                    384,
                    864,
                    654
                ],
                "color": "#FFAC3F"
            },
            {
                "name": "Ind",
                "data": [
                    642,
                    456,
                    983,
                    564
                ]
            },
            {
                "name": "Store",
                "data": [
                    832,
                    243,
                    646,
                    777
                ],
                "color": "#FF0000"
            }
        ]
    }
]

I want to return all the data inside "series" but doing something like say assigning this to a variable and then using data.series is not returning anything. 我想返回“系列”中的所有数据,但是要做类似将其分配给变量然后使用data.series的操作,但不会返回任何内容。 How would I do this using jquery or javascript? 我将如何使用jquery或javascript执行此操作? This data is actually getting returned in a jquery JSON ajax request like so.... 此类数据实际上是在jquery JSON ajax请求中返回的。

$.getJSON(url,  function(data) {

Your JSON is wrapped in an array. 您的JSON包装在数组中。 You want 你要

data[0].series

For a better visual, review this 为了获得更好的视觉效果,请查看此

var data = [{foo: "bar"}, {hello: "world"}];

To get foo , you would use 要获取foo ,您可以使用

data[0].foo; // "bar"

To get hello , you would use hello ,您可以使用

data[1].hello; // "world"

Considering you have 考虑到你有

var data = [{series: [...]}];

You need to use 您需要使用

data[0].series; // [...]
$.getJSON(url, function(data) {
    console.log(data[0].series)
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM