Tutorial :How would I read such a JSON structure?



Question:

{"some_id":      [      {"city":"Bellevue"},      {"state":"Washington"}      ]  }  


Solution:1

var theJSonString = '({"some_id": [ {"city":"Bellevue"}, {"state":"Washington"} ] })';  var x = eval(theJSonString);  alert(x.some_id[0].city); // will display "Bellevue"  


Solution:2

var json = {"some_id": [ {"city":"Bellevue"}, {"state":"Washington"} ] }  

json.some_id[0].city equals "Bellevue"

and

json.some_id[1].state equals "Washington"


Solution:3

And this (the json parser and stringifier from json.org) might help :) (check the link at the bottom of the page)


Solution:4

All current browsers support window.JSON.parse(). It takes a JSON formatted string and returns a Javascript object or array.

Demo: http://jsfiddle.net/ThinkingStiff/KnbAJ/

Script:

var json = '{"some_id":[{"city":"Bellevue"},{"state":"Washington"}]}'      object = window.JSON.parse( json );    document.getElementById( 'length' ).textContent = object.some_id.length;  document.getElementById( 'city' ).textContent = object.some_id[0].city;  document.getElementById( 'state' ).textContent = object.some_id[1].state;  

HTML:

length: <span id="length"></span><br />  some_id[0].city: <span id="city"></span><br />  some_id[1].state: <span id="state"></span><br />  

Output:

length: 2  some_id[0].city: Bellevue  some_id[1].state: Washington  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »