Getting Started with JSON (ii)

Source: Internet
Author: User
Tags compact

Background

Before the earliest, I have seen a string similar to this:

{"List": [
{"ArticleID": 7392749, "BlogId": 1158641, "Commentid": 2182295, "Content": "I am to test the comment drops, haha", "ParentID": 0, "posttime": " 2 minutes Ago "," replies ": null," UserName ":" Crifan "," Userface ":" Http://avatar.csdn.net/E/D/E/3_crifan.jpg "},
{"ArticleID": 7392749, "BlogId": 1158641, "Commentid": 2182254, "Content": "mark__", "ParentID": 0, "posttime": "52 minutes ago", " Replies ": null," UserName ":" Mars_tl "," Userface ":" Http://avatar.csdn.net/9/3/0/3_mars_tl.jpg "}]," page ": {" PageSize ": PageIndex": 1, "RecordCount": 0, "PageCount": 0}, "FileName": "7392749"}

Obviously, such a character, looks like there is a pattern, but just knew it was a string.

Later, gradually, learned that the original string, there is a specific format and regular, which is called the JSON string.

What is JSON

Then I went to check what JSON was.

Here, we first cite:

In the JSON wiki:

JSON(JAvascript Object Notation) is a lightweight data exchange language that is text-based and easy to read. Although JSON is a subset of JavaScript, JSON is a language-independent text format and uses some of the habits similar to that of the C language family.

Here, simple can be understood as:

A brief explanation of JSON
    • The JSON full name is JAvascript Object Notation, which is obviously derived from JavaScript. This is not the place to delve into this, know this.

    • JSON is a string of strings that have a certain syntax format;

    • JSON defines such a syntax format in order to facilitate the exchange of data. That is, some data, through the format of JSON, from one place, especially on the network, sent to another place, and then make the recipient, it is also easy to understand the relevant data.

and specific about the JSON syntax, that is, how to write, specifically what kind of format, no longer repeat, only as simple as possible to introduce:

The most basic syntax is:

The most basic syntax for JSON
    • Object, which is a variable name, a value, corresponding to the notation: {name:value}

    • List, there are multiple elements, the notation is: [Collection, collection]

The rest, according to the normal logic understanding, such as the string is two double quotation mark "", the number is 0 to 9 and so on.

Examples of JSON

Examples of wiki cited are:

?
123456789101112131415161718192021222324 {     "firstName": "John",     "lastName": "Smith",     "male": true,     "age": 25,     "address":      {         "streetAddress": "21 2nd Street",         "city": "New York",         "state": "NY",         "postalCode": "10021"     },     "phoneNumber":      [         {           "type": "home",           "number": "212 555-1234"         },         {           "type": "fax",           "number": "646 555-4567"         }     ] }

By this example, and even if the image is known, JSON is a structure is very clear, used to represent the data of a format.

Some considerations for JSON The JSON string sent is compact (non-indented)

It is necessary to note that, because of the invention of JSON, is for the purpose of data exchange, so, before sending the JSON string, is actually not what you see above, formatting, with indentation of the kind, are compact, there is no space between the characters,

As the first example of this article, send these compact data to others.

Format the JSON string (indented) with the Format tool for easy viewing

And the recipient, as a human, want to view the data, can clearly understand the data, will generally use some tools, such as notepad++ plug-in jsmin, to format,

The format of the JSON data we gave at the beginning is:

How the JSON is handled in the program

Many programs will encounter JSON strings, so it involves how to process JSON strings through a program, such as converting a JSON string into a corresponding dictionary class variable, and so on.

Obviously, of course, you can manually write code, a little bit of processing, but more obvious, inefficient.

And in reality, indeed, for different languages, there is a corresponding JSON-related library function, to be used to process the JSON string.

For these different languages, the corresponding JSON processing library, others JSON official website, has helped you to tidy up. See for yourself and learn how the corresponding JSON library is used.

which includes n languages, n JSON processing related libraries, I will not repeat to list, I go to the JSON official website to see.

Here, just to say, some of the JSON libraries I've used:

A library of the Python language that handles JSON: JSON

Previous toss-related, in the Python language processing JSON string, with the Python language built-in JSON library, relatively still very useful.

Sample code for how JSON is handled in related Python can be consulted:

Organize (dictionary, list, etc) variables into (beautiful, tree-shaped, indented, json-like) string output

Json.loads error in "Experience history" python, note the encoding of the JSON character to decode

In addition, sometimes, encountered some special situation, but also can use the BeautifulSoup library to help deal with:

Resolved JSON string parsed in HTML format with BeautifulSoup

working with JSON strings in C #

I've also been working with JSON strings in C # before.

Just then found that C # built-in without the relevant JSON library, but also need to install a third-party JSON library, feel too much trouble, so give up:

The implementation of "abandoned" C # converts a JSON string to a variable

Resolving JSON strings in "Resolved" C #

"Unresolved" A variable in C # that gets a list type from json.net parsed JSON

For the moment, the regular expression class regex is used to manually process and extract the required content.

Of course, if someone has a need, it's worth installing a third-party JSON library to handle the JSON string.

Some of the later toss, C #, has been able to handle JSON strings well.

Just still need to rely on JavaScriptSerializer in. NET 3.5+.

See:

"Resolved" in C #, the JSON string goes to the dictionary and gets the corresponding dictionary key value

Getting Started with JSON (ii)

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.