How to select the first property with an unknown name and the first element from an array in JSON

I actually have two questions, both probably simple, but for some odd reason I can't figure it out ... I also worked with JSON 100 times! but here is the JSON in question:

{"69256":{ "streaminfo":{ "stream_ID":"1025", "sourceowner_ID":"2", "sourceowner_avatar":"http:\/\/content.nozzlmedia.com\/images\/sourceowner_avatar2.jpg", "sourceownertype_ID":"1", "stream_name":"Twitter", "streamtype":"Social media" "appsarray":[] }, "item":{ "headline":"Charboy", "main_image":"http:\/\/content.nozzlmedia.com\/images\/author_avatar173212.jpg", "summary":"ate a tomato and avocado for dinner...", "nozzl_captured":"2010-05-12 23:02:12", "geoarray":[{ "state":"OR", "county":"Multnomah", "city":"Portland", "neighborhood":"Downtown", "zip":"97205", "street":"462 SW 11th Ave", "latitude":"45.5219", "longitude":"-122.682" }], "full_content":"ate a tomato and avocado for dinner tonight. such tasty foods. just enjoyable.", "body_text":"ate a tomato and avocado for dinner tonight. such tasty foods. just enjoyable.", "author_name":"Charboy", "author_avatar":"http:\/\/content.nozzlmedia.com\/images\/author_avatar173212.jpg", "fulltext_url":"http:\/\/twitter.com\/charboy\/statuses\/13889868936", "leftovers":{ "twitter_id":"tag:search.twitter.com,2005:13889868936", "date":"2010-05-13T02:59:59Z", "location":"iPhone: 45.521866,-122.682262" }, "wordarray":{ "0":"ate", "1":"tomato", "2":"avocado", "3":"dinner", "4":"tonight", "5":"tasty", "6":"foods", "7":"just", "8":"enjoyable", "9":"Charboy", "11":"Twitter", "13":"state:OR", "14":"county:Multnomah, OR", "15":"city:Portland, OR", "16":"neighborhood:Downtown", "17":"zip:97205" } } } } 

Question 1: How do I scroll through each element (69256) when the number is random? for example, item 1 is 123 , item2 is 646 ? For example, a regular JSON channel will have something like:

{'item':{'blah':'lorem'},'item':{'blah':'ipsum'}}

JS will like console.log(item.blah) return lorem , then ipsum in a loop

How can I do this when I do not know the first element of an object?

Question 2: How to select elements from a geoarray object? I tried: json.test.item.geoarray.latitude

and

json.test.item.geoarray['latitude']

+4
source share
3 answers

Question 1: How do I scroll through each element (69256) when the number is random?

You can get the property names of the JS object using the for in statement:

 for (var property in object) { if (object.hasOwnProperty(property)) { alert(property + '=' + object[property]); } } 

To get the first of JSON, you can do:

 var data; for (var property in json) { if (json.hasOwnProperty(property)) { data = json[property]; break; } } 

Question 2: How to select elements from a geoarray object?

geoarray is actually an array with one element. Use [0] to capture it.

 var latitude = data.item.geoarray[0].latitude; 

If in real life there are several elements, you will need to jQuery.each() over it using a simple for with index or using jQuery.each() .


To learn more about JSON, you may find this tutorial useful.

+9
source

You will need to use the (somewhat less well-known) for...in loop:

 for (id in feed) { if (feed.hasOwnProperty(id)) { // do something with feed[id] } } 

The hasOwnProperty check is for security only if something did the weird things of Object.prototype. You may not need this.

As the name says, geoarchy is really an array containing several elements. Use .geoarray[0].latitude to access the first item, etc.

+2
source

The order of the JSON properties is undefined, so there is no first element. Some implementations preserve the order in which they are defined, but this is not something you can count on.

+1
source

Source: https://habr.com/ru/post/1309620/


All Articles