Javascript 从 jquery 访问 json 数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4424518/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
accessing json data from jquery
提问by WWWIZARDS
I'm creating an ajax app using jQuery 1.4.2 and I've tried using using get(), post() and the ajax() method itself. My php service returns:
我正在使用 jQuery 1.4.2 创建一个 ajax 应用程序,我尝试使用 get()、post() 和 ajax() 方法本身。我的 php 服务返回:
[{"k":"label0","v":0.5},{"k":"label1","v":99.43},{"k":"label2","v":2.46},{"k":"label3","v":46.29},{"status":"OK"}]
in my success callback I have tried accessing as json.status and json[0][0] but it always returns "undefined". what am I doing wrong?
在我的成功回调中,我尝试以 json.status 和 json[0][0] 访问,但它总是返回“未定义”。我究竟做错了什么?
function getSysinfo(source) {
var json = null;
$.ajax({
url: source,
type: 'POST',
dataType: 'json',
success: function (data) {
json = eval("(" + data + ")");
$('#data').html(json.status);
alert(json[0][0]);
refreshChart(json);
},
error: function (request, status, error) {
alert("REQUEST:\t" + request + "\nSTATUS:\t" + status +
"\nERROR:\t" + error);
}
});
return json;
}
I've been googling this for days. How the heck do I access the returned data? any help would be appreciated.
这几天我一直在谷歌上搜索。我到底如何访问返回的数据?任何帮助,将不胜感激。
回答by andynormancx
To access that status value you would need:
要访问该状态值,您需要:
data[4].status
This is because it is an object stored in the the fifth element in an array, with status
being a property on the object.
这是因为它是一个存储在数组第五个元素中status
的对象,并且是该对象的一个属性。
回答by Marcel Hymanwerth
Your JSON-data looks like this:
您的 JSON 数据如下所示:
[
{
"k": "label0",
"v": 0.5
},
{
"k": "label1",
"v": 99.43
},
{
"k": "label2",
"v": 2.46
},
{
"k": "label3",
"v": 46.29
},
{
"status": "OK"
}
]
You would have to read your status using
你必须使用阅读你的状态
json[4].status
with the 4
as a magical number or length-1
- not desirable. I would consider modifying your servers response to something more useful like this:
与4
作为一个神奇的数字或length-1
- 不可取。我会考虑将您的服务器响应修改为更有用的东西:
{
"status": "OK",
"entries": [ ... ] // add your data here
}
回答by Lorenzo
In your success callback try:
在您的成功回调中尝试:
var parsed = $.parseJSON(data);
$.each(parsed, function (i, jsondata) {
alert( jsondata.k );
alert( jsondata.v );
});
回答by nategood
You don't need the eval("("+data+")");
. jQuery is automatically parsing the JSON response for you because you specified dataType:'json'
你不需要eval("("+data+")");
. jQuery 会自动为您解析 JSON 响应,因为您指定了dataType:'json'
From the jQuery docs for dataType:
来自数据类型的 jQuery 文档:
"json": Evaluates the response as JSON and returns a JavaScript object. In jQuery 1.4 the JSON data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown. (See json.org for more information on proper JSON formatting.)
“json”:将响应评估为 JSON 并返回一个 JavaScript 对象。在 jQuery 1.4 中,JSON 数据以严格的方式解析;任何格式错误的 JSON 都会被拒绝并引发解析错误。(有关正确 JSON 格式的更多信息,请参阅 json.org。)
回答by kobe
no need to use eval any more use below code which can be more for json
不再需要使用 eval 使用下面的代码,这对于 json 来说可能更多
$.getJSON(url+query,function(json){
$.each(json,function(i,value){
});
});
回答by Dr.Molle
nategood already wrote that you don't need do do anything with data, it's already an object.
nategood 已经写到你不需要对数据做任何事情,它已经是一个对象。
In this case it's an array, if you like to access the status, you need to retrieve it from the last item of the data-array(that's where you'll find it in this array):
在这种情况下,它是一个数组,如果您想访问状态,则需要从数据数组的最后一项中检索它(您将在此数组中找到它):
data[data.length-1].status
But maybe you should think about another structure of your JSON, it doesn't look very comfortable.
但也许你应该考虑一下你的 JSON 的另一种结构,它看起来不太舒服。
Something like that:
类似的东西:
{
"items":[
{"k":"label0","v":0.5},
{"k":"label1","v":99.43},
{"k":"label2","v":2.46},
{"k":"label3","v":46.29}
],
"status":"OK"
}
...should be easier to handle, because you can simply access data.status instead of first looking where you may find it inside the response(what may be error-prone ).
...应该更容易处理,因为您可以简单地访问 data.status 而不是首先查看您在响应中可能找到的位置(可能容易出错)。
回答by vitch
The data parameter is the decoded JSON as you can see in this example:
data 参数是解码后的 JSON,如您在此示例中所见: