jQuery 如何通过 JSON 数组(如查询)获取值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8177993/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 10:10:30  来源:igfitidea点击:

How can I get value through JSON array like query

jqueryhtmljson

提问by user1053120

Sorry for my English. I have a Json Array :

对不起我的英语不好。我有一个 Json 数组:

"computers":
[
    {
        "id": 1,
        "type": "laptop",
        "manufactor":
        [
            {
                id: "m_1",
                name: "HP"
            },
            {
                id: "m_2",
                name: "Sony"
            }
        ] 
    },
    {
        "id": 2,
        "type": "desktop",
        "manufactor":
        [
            {
                id: "m_3",
                name: "Apple"   
            },
            {
                id: "m_4",
                name: "Tiger"   
            }
        ] 
    }
]

Could you tell me how can I do it like this: load computers data >> get 2 computer id 1 and 2 >> choose computer id = 1 >> load the manufactor of that computer. I can do it with mysql query but in json i don't know how to do that. Thanks for Suggestion!

你能告诉我我该怎么做:加载计算机数据>>获取2台计算机ID 1和2>>选择计算机ID = 1>>加载该计算机的制造商。我可以用 mysql 查询来做到这一点,但在 json 中我不知道该怎么做。感谢您的建议!

Edit: I want to retrieve the manufacturer of the computer with ID - 1 from the JSON object above.

编辑:我想从上面的 JSON 对象中检索 ID 为 1 的计算机的制造商。

Thank to everyone for helping me out. I am going to create a page working with JSON look like DOJOX Rolling list, would you give me example or suggestion how to do like that ? thank you:D

感谢大家帮助我。我将创建一个使用 JSON 的页面,看起来像 DOJOX 滚动列表,你能给我举个例子或建议如何做吗?谢谢:D

回答by Kevin Anthony Oppegaard Rose

Using:

使用:

computers[0].manufactor[0].name

Will return "HP".

将返回“HP”。

If you change it to:

如果将其更改为:

computers[0].manufactor[1].name

You will get "Sony".

你会得到“索尼”。

Changing it further to:

将其进一步更改为:

computers[1].manufactor[0].name

will return "Apple".

将返回“苹果”。

And finally:

最后:

computers[1].manufactor[1].name

returns "Tiger".

返回“老虎”。

Look here for moreinformation on how to manipulate JSON objects with Javascript.

有关如何使用 Javascript 操作 JSON 对象的更多信息,请查看此处。

回答by Shailesh Jangir

Say the given JSON data is in var data = {your give json string here};and you want to access the computersarray from data.

假设给定的 JSON 数据在 var 中,data = {your give json string here};并且您想computersdata.

Array computersis accessed using

computers使用数组访问

alert(data.computers);

For accessing the manufacturer, use the following:

要访问制造商,请使用以下内容:

for (var i in data.computers){
var id= i.id; /*returns id*/
var type = i.type;/*returns type*/
var manufactor = i.manufactor;/*returns munufactor i.e. an array*/
/*MANUFACTOR IS ARRAY SO YOU MAY USE*/
for (var j in manufactor){
    var m_name = j.name; /*returns manufactor's name*/
    var m_id = j.id; /*returns manufactor's id*/
}}

That is used if you have unknown number of computers and manufacturer in an array.

如果数组中有未知数量的计算机和制造商,则使用该方法。

回答by Rohan

computer[1].id read more : http://www.json.org/fatfree.html

计算机 [1].id 阅读更多信息:http: //www.json.org/fatfree.html

回答by Elias Hossain

var jsonData = yourJsonData;
//Looking through computers
for(var i = 0; i<jsonData.computers.length; i++)
{
var id= jsonData.computers[i].id; // Get Id
var type = jsonData.computers[i].type; // Get Type
var manufacturers = jsonData.computers[i].manufactor; // Get manufacturers array
//Looking through manufacturer in computers
for (var j=0; j< manufacturers.length; j++ ){
   var manufacturer_id = manufacturers[j].id;   // Get manufacturer's Id
   var manufacturer_name = manufacturers[j].name;  // Get manufacturer's name
  }
}