Json Getting Started example "3"

Source: Internet
Author: User

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"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.