简体   繁体   中英

Object retrieval from JSON using Jquery / JS

  1. I have a JSON file at /path/to/json containing:

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

    I'm using this JQuery expression:

     $.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()?

  2. I have another JSON file containing:

     {"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?

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

data.a.s[0].l

To get the second item in the s array

data.a.s[1].l

Or use a for loop

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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