简体   繁体   English

如何使用JavaScript访问JSON格式的数据

[英]How to access data in JSON format using javascript

I'm trying to figure out on how to access data in JSON format and have gone a whole day searching for ways but I still can't find a solution to fit my needs. 我试图弄清楚如何以JSON格式访问数据,并花了一整天的时间寻找方法,但是我仍然找不到适合我需求的解决方案。 The closest relative question to my problem is this question but to no avail. 与我的问题最接近的相对问题是这个问题,但无济于事。

Basically I am retrieving data from $.ajax() which returns in JSON format. 基本上我是从$.ajax()检索数据,该数据以JSON格式返回。

[{"v":"233","pv":"1.83","avd":"00:01:58","nv":"82.83%","br":"75.11%"},
{"v":"17","pv":"3.65","avd":"00:08:31","nv":"70.59%","br":"58.82%"},
{"v":"9","pv":"2.22","avd":"00:01:51","nv":"0.00%","br":"44.44%"}]
[{"date":"2013-02-01","visits":63},
{"date":"2013-02-02","visits":30}]

My problem is how can I access the elements inside the JSON, say I want to get all values of "nv" or all values of "date" on the second bracket, in javascript? 我的问题是我该如何访问JSON中的元素,比如说我想在JavaScript的第二个括号中获取“ nv”的所有值或“ date”的所有值? I Am new at things like these so am not familiar with the terminologies, sorry for that. 我对这些事情还是陌生的,因此对术语不熟悉,对此感到抱歉。

Below is my code: 下面是我的代码:

var Data = $.ajax({
    url: url,
    type: 'POST',
    dataType:"json",
    async: false
}).responseText;

console.log(Data);

url is a variable that is passed inside my function in case you might ask. url是一个变量,如果您可能要问的话,该变量会在我的函数中传递。

Update : See Anthony Grist's comment on your question, I missed the fact that your JSON is invalid. 更新 :查看Anthony Grist对您的问题的评论,我错过了您的JSON无效的事实。 Since he hasn't posted an answer, I'll pick it up. 由于他还没有发布答案,所以我来接听。

Your JSON is invalid because you're returning two separate arrays, this one: 您的JSON无效,因为您要返回两个单独的数组,这个数组是:

[{"v":"233","pv":"1.83","avd":"00:01:58","nv":"82.83%","br":"75.11%"},
{"v":"17","pv":"3.65","avd":"00:08:31","nv":"70.59%","br":"58.82%"},
{"v":"9","pv":"2.22","avd":"00:01:51","nv":"0.00%","br":"44.44%"}]

and this one: 还有这个:

[{"date":"2013-02-01","visits":63},
{"date":"2013-02-02","visits":30}]

You can't do that, because the top level of a JSON document must be one thing (an object or an array). 您不能这样做,因为JSON文档的顶层必须是件事(对象或数组)。

You could return an object with properties for each array: 您可以为每个数组返回一个具有属性的对象:

{
"vdata":
    [{"v":"233","pv":"1.83","avd":"00:01:58","nv":"82.83%","br":"75.11%"},
     {"v":"17","pv":"3.65","avd":"00:08:31","nv":"70.59%","br":"58.82%"},
     {"v":"9","pv":"2.22","avd":"00:01:51","nv":"0.00%","br":"44.44%"}
    ],
"datedata":
    [{"date":"2013-02-01","visits":63},
     {"date":"2013-02-02","visits":30}
    ]
}

After parsing (see below), you can access that data like this: 解析后(见下文),您可以像这样访问该数据:

console.log(data.vdata[0].v); // "233"
console.log(data.datedata[0].date); // "2013-02-01"

Or an array with two slots, with each slot having one of your arrays in it: 或具有两个插槽的阵列,每个插槽中都有一个阵列:

[
    [{"v":"233","pv":"1.83","avd":"00:01:58","nv":"82.83%","br":"75.11%"},
     {"v":"17","pv":"3.65","avd":"00:08:31","nv":"70.59%","br":"58.82%"},
     {"v":"9","pv":"2.22","avd":"00:01:51","nv":"0.00%","br":"44.44%"}
    ],
    [{"date":"2013-02-01","visits":63},
     {"date":"2013-02-02","visits":30}
    ]
]

After parsing (see below), you can access that data like this: 解析后(见下文),您可以像这样访问该数据:

console.log(data[0][0].v); // "233"
console.log(data[1][0].date); // "2013-02-01"

Personally, I prefer using an object, since then it's clear which array I'm accessing. 就我个人而言,我更喜欢使用一个对象,因为这样很清楚我正在访问哪个数组。


Original answer: 原始答案:

jQuery will parse the JSON into an object for you and pass that to the success function, which you can then access just like any other object. jQuery将为您解析JSON并将其传递给success函数,然后您就可以像访问其他任何对象一样访问该函数。 In your case, the top level is an array. 在您的情况下,顶层是一个数组。 So: 所以:

$.ajax({
    url: url,
    type: 'POST',
    dataType:"json",
    async: false,
    success: function(data) {
        // Use the line from above that suits the way
        // you updated your JSON structure
    }
});

Side note: async: false is deprecated and will be removed at some point. 旁注: async: false使用async: false ,并将在某些时候将其删除。 It's generally not a good idea to do synchronous ajax requests, it tends to lock up the UI of the browser during the request. 同步ajax请求通常不是一个好主意,它会在请求期间锁定浏览器的UI。 Instead, just structure your code to continue processing when the success callback is triggered. 相反,只要构造代码以在触发success回调时继续处理即可。

If I understand your problem you need to access the same key for all objects in this array. 如果我了解您的问题,则需要为该数组中的所有对象访问相同的键。

There is no direct method to do that, you have to iterate through all objects in this array and then find the desired key in each of those objects. 没有直接的方法可以执行此操作,您必须遍历此数组中的所有对象,然后在每个这些对象中找到所需的键。

JSON.parse() will convert this string into a Javascript Object (JSON) JSON.parse()会将字符串转换为Javascript对象(JSON)

var myData = JSON.parse(Data);

for(var i = 0; i < myData.length; i++) {
    console.log("This is the nv value of the " + i + " object: " + myData[i].nv);
}

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

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