Summary of the use of "Js" JSON objects and JSON characters

Source: Internet
Author: User

JSON object/JSON string differences

Throw one of the most common questions: what is a "JSON object", what is a "JSON string", and what is the difference between them?

  Nonsense not much to say, directly on the code.  

1.JSON objects:

JavaScript object literal notation var obj = {  "name": "Jay", "Age": "+"};  //json object notation var json = {name  : "Jay", Age  : "18"};

 

In the usual application, these two ways to remove the object's property name with no "quotation marks", there is no difference, using Console.log () in the console output, the results are as follows:

1Console.log (obj);//Output Object literal2Console.log (JSON);//output JSON object3------------------------------------------4 //Show Results5 //object literal6 Object7Age:188Name: "Jay"9 __proto__: ObjectTen  One //JSON Object A Object -Age:18 -Name: "Jay" the __proto__: Object -  -     

It turns out that these two kinds of writing are essentially the same in JS, and what we normally call "JSON objects " is usually defined in the two ways described above. It is used just like accessing the properties of a JS object, through the "." To quote:

Console.log (Obj.name);  Jayconsole.log (json.age);//18

  

2.JSON string

var jsonstr = ' {' name ': ' Jay ', ' Age ': ' 18 '} ';

As the name implies,a JSON string is essentially a "string".

When a programmer mentions a string, the first thought might be a string of characters wrapped in "quotes", such as "ABC" and "Def". So we can distinguish between JSON strings and JSON objects, the JSON object in the literal notation wrapped in quotation marks, which is the JSON string.

JSON objects/JSON strings convert to each other

1.JSON Object-----------------------------"JSON string:

①json.stringify (obj) ---common---

How to use
var obj = {name: "Jay", Age: "};var" str = json.stringify (obj); alert (str); Display Result: ' {' name ': ' Jay ', ' Age ': ' 18 '} '

  

②json.tojsonstring ()

Find a half-day, specifically did not find how to use, but also to introduce the package, forget it, abandoned (ruthless).

  

2.JSON string-----------------------------"JSON object:

①json.parse () ---popular---

var str = ' {' name ': ' Jay ', ' Age ': ' "} '; var obj = Json.parse (str); Console.log (obj); Display Result: {name: "Jay", Age: "18"}

②eval (' (' +str+ ') ')

var str = ' {' name ': ' Jay ', ' Age ': ' "} '; var obj = eval (' (' +str+ ') '); Console.log (obj); Display Result: {name: "Jay", Age: "18"}

  

③$.parsejson (str)

var str = ' {' name ': ' Jay ', ' Age ': ' "} '; var obj = $.parsejson (str); Console.log (obj);
Display Result: {name: "Jay", Age: "18"}

Application Scenario Example: Submit data to Backend (PHP code)

Previous paragraph HTML:

<table>    <tr class= "Student" >        <td> Name: <input type= "text" name= "name" ></td>        <td> Age: <input type= "text" Name= "ages" ></td>    </tr>    <tr class= "student>        <td> Name: <input type= "text" name= "name" ></td>        <td> Age: <input type= "text" Name= " Age "></td>    </tr></table>        

  

Front-end JS:

//transfer Multiple sets of student information to the backendvarStudents = [];$(". Student"). each (function(index, Element) {varName= $ (Element). Find ("Input[name=name]"). Val (). Trim (); varAge = $ (Element). Find ("Input[name=age]"). Val (). Trim ()varTMP ={name:name, age:age} student.push (TMP);})varform =NewFormData (); Form.append ("Students", Json.stringify (students)); $. (Ajax () {type:"POST", Data:form, Url:posturl, ...})

PHP Code:

 

$students = $_request[' students '); // The second argument is true for the array type, omitting the second argument, expressed as the object type $students = Json_decode ($student,true); // Output Student Information  ' student: '. $item [' name ']. ' The age is '. $item [' age '];}

  

 

Summary of the use of "Js" JSON objects and JSON characters

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.