如何使用 jquery 和 json 访问特定的 json 对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3203417/
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 can I access a specific json object using jquery and json?
提问by Duffy Dolan
I'm having a problem trying to return a specific record using jQuery and JSON. I'm passing a variable to the function and want to output only the data matching the id I've set when the function is executed.
我在尝试使用 jQuery 和 JSON 返回特定记录时遇到问题。我正在向函数传递一个变量,并且只想输出与我在执行函数时设置的 id 匹配的数据。
Right now, my function looks like this:
现在,我的函数如下所示:
function getEffectsData(myNum){
$.getJSON("jsonscript.php", { id: +myNum },function(data){
var x = data.x;
//DO SOME STUFF
});
}
The JSON output looks like this:
JSON 输出如下所示:
{"stuff": [ { "id":1, "x":3, "y":6, "z":-6 },{ "id":2, "x":2, "y":7, "z":-3 }]}
{"stuff": [ { "id":1, "x":3, "y":6, "z":-6 },{ "id":2, "x":2, "y":7, "z":-3 }]}
The only thing I know is that the correct variable is being passed to the function. After that, nothing happens. Very new at this, so any help is greatly appreciated.
我唯一知道的是正确的变量被传递给函数。之后,什么也没有发生。在这方面非常新,因此非常感谢任何帮助。
EDIT: I appreciate everybody's input on this. To clarify, I am trying to return the data from only the object that's id matches myNum. I want to be able to access all of the data from that object, but only that object. I have no idea where that object will be in the array. I simplified the ids and the data in my question to help clarify the problem.
编辑:我感谢大家对此的意见。为了澄清,我试图仅从 id 与 myNum 匹配的对象返回数据。我希望能够访问该对象的所有数据,但只能访问该对象。我不知道该对象在数组中的位置。我简化了问题中的 ID 和数据以帮助澄清问题。
Thanks again.
再次感谢。
回答by gblazex
You can access x from your JSON like this:
您可以像这样从 JSON 访问 x:
var x1 = data.stuff[0].x; // the first object's x key
var x2 = data.stuff[1].x; // the second object's x key
Because your JSON tree looks like this:
因为您的 JSON 树如下所示:
{ // base object
"stuff": [ // array
{ // 0.: object
"id": 1, // key => value
"x" : 3, // key => value
"y" : 6, // key => value
"z" : -6 // key => value
},
{ // 1.: object
"id": 2, // key => value
"x" : 2, // key => value
"y" : 7, // key => value
"z" : -3 // key => value
}]
}
So with data.stuff[0].x
you select stuff
object then it's first
element and then its x
key.
因此,data.stuff[0].x
您选择stuff
对象,然后选择它的first
元素,然后选择它的x
键。
But If you want to handle all the x
key for example, then you need to loop through the stuff
array by a simple for
loop or the $.each
method.
但是如果你想处理所有的x
键,那么你需要stuff
通过一个简单的for
循环或$.each
方法来遍历数组。
UPDATE
更新
As for your question. If you want to get the object with id myNum
you have 2 possibilities:
至于你的问题。如果您想获取带有 id 的对象,myNum
您有两种可能性:
if you possess
jsonscript.php
you can send the data only with the correct id from the server, because you pass the id to it by{ id: +myNum }
which is the second parameter ofgetJSON
You loop through the data until you find the object with the correct id
var object; $.each(data.stuff, function(i, obj) { if (obj.id === myNum) object = obj; }); // now object is the one with the right id
如果您拥有
jsonscript.php
,则只能使用来自服务器的正确 id 发送数据,因为您将 id 传递给它,{ id: +myNum }
这是第二个参数getJSON
您遍历数据,直到找到具有正确 id 的对象
var object; $.each(data.stuff, function(i, obj) { if (obj.id === myNum) object = obj; }); // now object is the one with the right id
回答by Nick Craver
It's an array under stuff
, so you would need something like this:
它是 下的一个数组stuff
,所以你需要这样的东西:
function getEffectsData(myNum){
$.getJSON("jsonscript.php", { id: +myNum },function(data){
$.each(data.stuff, function(i, obj) {
var x = obj.x;
//DO SOME STUFF
});
});
}
If you're after the very first x
you can do data.stuff[0].x
, but I think you'd want to loop through here.
如果你在第一个之后x
你可以做data.stuff[0].x
,但我认为你想在这里循环。
回答by jAndy
should be
应该
var x = data[0].x;
or
或者
var x = data[1]['x'];
for instance.
例如。