Home >Web Front-end >JS Tutorial >Json format analysis

Json format analysis

高洛峰
高洛峰Original
2016-12-17 15:12:221476browse

The format of the json structure is a collection of several key/value (key, value) pairs. This collection can be understood as a dictionary (Dictionary), and each key/value pair can be understood as an object (Object). The key in a key/value pair is generally a string, and the value can be a basic type such as string, double, or int. It can also be a nested key/value pair, or it can be an array or the data in the array. The type can be a basic type, or a key/value pair. It can be seen that key/value is nothing originally, but it will be confusing if it is nested too much. Let's give a specific example to illustrate. Note: This code is only used as an example and will not run correctly.

var testJson = {                      
                            "Name" :      "奥巴马" ,
                            "ByName" :    ["小奥","小巴","小马"],
                            "Education" : {
                                           "GradeSchool" :  "华盛顿第一小学",
                                           "MiddleSchool" : ["华盛顿第一初中" , "华盛顿第一高中"],
                                           "University" :  { 
                                                              "Name" : "哈佛大学",
                                                              "Specialty" : ["软件工程","会计"]
                                                            }
                                      }
                        }

The variable testJson is a json object. The testJson object includes three key/value pairs.

The first key/value pair: The key (key) is "Name", and its corresponding value (value) is "Obama", that is, testJson["Name"] == "Obama"

The second key/ Value pair: The key is "ByName", the value is an array, which is a collection of strings. If necessary, the elements in the array can also be key/value pairs.

The third key/value pair: The key is "Education" and the value is a Json object. The json object includes three key/value pairs. This is nesting. . .

Summary: A json object is a collection of several key/value pairs. The key is string and the value can be a basic type, or a nested Json object, or an array (the elements in the array can be of basic type, or json object, you can continue to nest).

Get the name: testJson["Name"]

Get the first alias: testJson["ByName"][0]. The value corresponding to the key "ByName" of testJson is a string array

Get the primary school name: testJson["Education"]["GradeSchool"] , Get the university major: testJson["Education"]["University"][" Specialty"][0]

The following is an example:

Define a string that meets the json format requirements:

string testJson = "{\"Name\" : \"奥巴马\",\"ByName\" : [\"小奥\",\"小巴\",\"小马\"],\"Education\":{\"GradeSchool\" : \"华盛顿第一小学\",\"MiddleSchool\" : [\"华盛顿第一初中\" , \"华盛顿第一高中\"], \"University\" :{ \"Name\" : \"哈佛大学\", \"Specialty\" : [\"软件工程\",\"会计\"]}}}";

Then you need to use the string as a parameter to create a new JsonObject object. Microsoft's own class library System.Json, and then add the namespace using System.Json;

The main code is just one sentence: JsonObject js = JsonObject.Parse(testJson); Use the string testJson as the parameter to create a new JsonObject object. Through monitoring, we can see that the content in js is as expected. You should be able to figure out a lot of things from the picture below

Json format analysis

Insert an additional sentence: js["Education"]["University"][ The content of "Specialty"] is {[ "Software Engineering", "Accounting"]},

But the value of js["Education"]["University"]["Specialty"].Contains("Software Engineering") is false. Figure out the reasons yourself



For more articles related to Json format analysis, please pay attention to the PHP Chinese website!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:json formatNext article:json format