JSON arrays, JSON objects, arrays of differences and basic operations collation

Source: Internet
Author: User
Tags array to string string to json

JSON syntax rules JSON syntax is a subset of the JavaScript object representation syntax. Data in the name/value pairsdata is delimited by commas to hold the object square brackets to save the array JSON name/value pairsthe writing format for JSON data is: Name/value pairs. Names in name/value pairs are written in front (in double quotes), value pairs are written behind (also in double quotes), separated by colons:"FirstName ":" John "JSON values JSON values can be: numeric (integer or floating-point) strings (in double quotes) logical VALUES (trueOrfalseArray (in square brackets) object (in curly braces)NULLJSON is normally made up of objects and arrays, and objects are manipulated using the object's methods. Arrays are used to manipulate the array method. Arrayvarourcountry=[["Beijing"],["Shanghai"],["Hefei", "Wuhu", "Bangbu"];//ArrayAlert (ourcountry[2][1]); JSON ObjectvarStatus_process = {                "Name5": ' Idle period ',                "Name1": ' Seeding ',                "Name2": ' Seedling ',                "Name3": ' Growing season ',                "Name4": ' Harvest Time '} alert (status_process["Name5"]); alert (status_process.name5);//Two of them: Idle periodJSON string The so-called JSON string, meaning that the value of the string variable is the same as the JSON format, but not a JSON object, such as:varS1= "{";varS2 = "' name5 ': ' Idle period ', ' name1 ': ' Sowing time ', ' name2 ': ' Seedling ', ' name3 ': ' Growth period ', ' name4 ': ' Harvest Time '";varS3= "}";varstatus_process=s1+s2+S3; JSON array traversal Packjson= [        {"Name": "Zhang", "Password": "123"},        {"Name": "Li", "Password": "456"}]; for(varPinchPackjson) {//when traversing a JSON array, this writes p as the index, 0,1Alert (Packjson[p].name+ " " +packjson[p].password);}//or for(vari = 0; i < packjson.length; i++) {alert (Packjson[i].name+ " " +Packjson[i].password);} Operation of the JSON array1. Definevarjosnarry=[];2. Add Jsonarry.push (jsonobj);3. DeleteDeleteJsonarry[0];splice (index,length):4. Iterate through the array A.jquery$.each (Jsonarry,function(index,item) {varUserid=Item.userid; }); B. General Modalities for(vari=0;i<jsonarry.length;i++){    varUserid=Jsonarry[i].userid; }c. Special Methods for(varIteminchJsonarry) {     varUserid=jsonarry[item].userid;//Note: The item here is not an array item, but an index of the array item}json Object Traversal Myjson= {"Name": "Ji", "password": "123"}; for(varPinchMyjson) {//Traverse each key/value pair of JSON objects, p is keyAlert (P+ " " +myjson[p]);} Strings and JSON objects and arrays of JSON objects are converted to each othervarJsonstr = "{\" userid\ ": \" 001\ "}";//JSON Object StringvarJsonarrystr = "[{\" userid\ ": \" 001\ "},{\" userid\ ": \" 002\ "}]";//JSON Array stringvarJsonobj = Json.parse (JSONSTR);//string to JSON objectvarJsonarry = Json.parse (JSONARRYSTR);//string to JSON arrayvarJsonstr = Json.stringify (jsonobj);//JSON object to stringvarJsonarrystr=json.stringify (Jsonarry);//JSON array to stringalert (JSONSTR); alert (jsonarrystr); alert ( Jsonobj.userid); alert (jsonarry[0][' userId ');

JSON arrays, JSON objects, arrays of differences and basic operations collation

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.