Javascript and jquery get values by ID.
<script>var txt1 = [{"Cityid": "CityName": "Xian", "Provinceid": +, "Cityorder": 1}, {"Cityid": +, "CityName" : "Guangzhou", "Provinceid": "Cityorder": 1}];$ ("#a1"). html (txt1[0]. CityName); document.getElementById ("A1"). InnerHTML = Txt1[0]. cityname;//the above two kinds of writing results are the same. Gets the value by ID. </script><body> <p> City: <span id= "A1" ></span><br/> </p> </body>
Json Object Processing:
<script>
var J1 = {"FirstName": "John", "LastName": "Doe"}; var j11 = j1.firstname; alert (J11);
</script>
JSON array:
<script> var Aa1 = { "Employees": [ {"FirstName": "John", "LastName": "Doe"}, {"FirstName" : "Anna", "LastName": "Smith"}, {"FirstName": "Peter", "LastName": "Jones"} ] }; $ ("#a1"). HTML (aa1.employees[1].firstname); $ ("#a2"). HTML (aa1.employees[1].lastname);</script><body> <p> code: <span id= "A1" ></span><br/> Name: <span id= "A2" ></span><br/> </p></body>
Json string:
Because JSON syntax is a subset of JavaScript syntax, the JavaScript function eval () can be used to convert JSON text to JavaScript objects.
The eval () function uses the JavaScript compiler to parse the JSON text and then generate the JavaScript object. You must enclose the text in parentheses in order to avoid syntax errors:
<script>var txt = ' {' Employees ': [' + ' {' firstName ': ' Bill ', ' lastName ': ' Gates '}, ' + ' {' firstName ': ' George ', ' LA Stname ":" Bush "}, ' + ' {" FirstName ":" Thomas "," LastName ":" Carter "}]} '; var jtxt = eval (" ("+ txt +");; $ ("#a1"). HTML (jtxt.employees[1].firstname); $ ("#a2"). HTML (jtxt.employees[1].lastname); </script><body ><p> No.: <span id= "A1" ></span><br/> Name: <span id= "A2" ></span><br/></ P></body>
Json Getting Started example "3"