jQuery 如何访问 JSON 对象名称/值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10895306/
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
How to access JSON Object name/value?
提问by RollerCosta
function (data) {
//add values based on activity type
//data = JSON.parse(data);
//alert(abc.Phone1);
alert(data.myName)
alert(data.toString());
if (activityType == "Phone") {
}
return;
},
As you can see this callback function of $.ajax
taking JSON
data from controller.
如您所见,这个从控制器$.ajax
获取JSON
数据的回调函数。
For example:
例如:
[{"name":"myName" ,"address": "myAddress" }]
[{"name":"myName" ,"address": "myAddress" }]
In this case my first alert giving me undefined and second/third alert popup comes up with:
在这种情况下,我的第一个警报给了我未定义和第二/第三个警报弹出窗口:
[{"name":"myName" ,"address": "myAddress" }]
[{"name":"myName" ,"address": "myAddress" }]
How can I access value by name so that my first alert filled out with myName
which is value of name
?
如何按名称访问值,以便我的第一个警报填写myName
哪个值name
?
回答by thecodeparadox
In stead of parsing JSON you can do like followng:
除了解析 JSON,您还可以执行以下操作:
$.ajax({
..
dataType: 'json' // using json, jquery will make parse for you
});
To access a property of your JSON do following:
要访问 JSON 的属性,请执行以下操作:
data[0].name;
data[0].address;
Why you need data[0]
because data is an array, so to its content retrieve you need data[0]
(first element), which gives you an object {"name":"myName" ,"address": "myAddress" }
.
为什么需要,data[0]
因为 data 是一个数组,所以要检索它的内容,您需要data[0]
(第一个元素),它为您提供一个 object {"name":"myName" ,"address": "myAddress" }
。
And to access property of an object rule is:
访问对象规则的属性是:
Object.property
or sometimes
或有时
Object["property"] // in some case
So you need
所以你需要
data[0].name
and so on to get what you want.
data[0].name
等等以获得你想要的。
If you not
如果你不
set dataType: json
then you need to parse them using $.parseJSON()
and to retrieve data like above.
设置dataType: json
然后您需要使用解析它们$.parseJSON()
并检索如上所述的数据。
回答by U.P
The JSON you are receiving is in string. You have to convert it into JSON object You have commented the most important line of code
您收到的 JSON 是字符串。您必须将其转换为 JSON 对象您已注释掉最重要的代码行
data = JSON.parse(data);
Or if you are using jQuery
或者,如果您正在使用 jQuery
data = $.parseJSON(data)
回答by Mahendran Sakkarai
If you response is like {'customer':{'first_name':'John','last_name':'Cena'}}
如果你的回应是这样的 {'customer':{'first_name':'John','last_name':'Cena'}}
var d = JSON.parse(response);
alert(d.customer.first_name); // contains "John"
Thanks,
谢谢,
回答by Nicola Peluchetti
You should do
你应该做
alert(data[0].name); //Take the property name of the first array
and not
并不是
alert(data.myName)
jQuery should be able to sniff the dataType for you even if you don't set it so no need for JSON.parse.
即使您没有设置它,jQuery 也应该能够为您嗅探 dataType,因此不需要 JSON.parse。
fiddle here
在这里摆弄
回答by Kabilan S
Try this code..
试试这个代码..
function (data) {
var json = jQuery.parseJSON(data);
alert( json.name );
}
回答by The System Restart
I think you should mention dataType: 'json'
in ajax config and to access that value:
我认为您应该dataType: 'json'
在 ajax 配置中提及并访问该值:
data[0].name
回答by Mansuri Nurulhuda
You might want to try this approach:
您可能想尝试这种方法:
var str ="{ "name" : "user"}";
var jsonData = JSON.parse(str);
console.log(jsonData.name)
//Array Object
str ="[{ "name" : "user"},{ "name" : "user2"}]";
jsonData = JSON.parse(str);
console.log(jsonData[0].name)
回答by Butifarra
Here is a friendly piece of advice. Use something like Chrome Developer Toolsor Firebugfor Firefox to inspect your Ajax calls and results.
这是一个友好的建议。使用Chrome Developer Tools或Firebugfor Firefox 之类的工具来检查您的 Ajax 调用和结果。
You may also want to invest some time in understanding a helper library like Underscore, which complements jQuery and gives you 60+ useful functions for manipulating data objects with JavaScript.
您可能还想花一些时间来理解像Underscore这样的辅助库,它补充了 jQuery,并为您提供了 60 多个使用 JavaScript 操作数据对象的有用函数。