简体   繁体   English

使用Jquery / JS从JSON检索对象

[英]Object retrieval from JSON using Jquery / JS

  1. I have a JSON file at /path/to/json containing: 我在/ path / to / json有一个JSON文件,其中包含:

     {"a": {"s": [{"l": "PPP"}]}} 

    I'm using this JQuery expression: 我正在使用此JQuery表达式:

     $.ajax({ url: '/path/to/json', dataType: 'json', async: false, success: function(data) { $.each(data.as, function(index,element) { lines=element.l; }) } }); 

    How do I return the value at asl without using $.each()? 如何不使用$ .each()返回asl的值?

  2. I have another JSON file containing: 我还有另一个JSON文件,其中包含:

     {"a": {"s": [ { "l": "PPP", "qqq": "aaa" }, { "l": "FFF", "qqq": "bbb" } ]}} 

    This is a file. 这是一个文件。 How do I assign the contents of the file to a variable then search within this json for the object who's 'l' attribute is 'PPP' then retrieve that object's 'qqq' value? 如何将文件的内容分配给变量,然后在json中搜索“ l”属性为“ PPP”的对象,然后检索该对象的“ qqq”值?

You can use the array index, if you know the index of item to be fetched 如果知道要提取的项目的索引,则可以使用数组索引

To get the first item in the s array 获取s数组中的第一项

data.a.s[0].l

To get the second item in the s array 获取s数组中的第二项

data.a.s[1].l

Or use a for loop 或使用for循环

for(var i= 0; i<data.a.s.length; i++){
    lines = data.a.s[i].l
}

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

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