"Java" JSON format parsing

Source: Internet
Author: User

Achieve a small function, all stumbling. No confidence. Feeling and become a cycle of death, feel the knife to cut wood, so with a semi-finished scrap knife piece of cutting. The last thing to cut out of the mess, oneself also took a great effort.

The function is to first query a string of data from the database, then organize it into JSON format and return it to jsp,jsp to display it to the front-end interface. But not now, the reason should be returned is not the JSON format, so the front-end interface is not known.

JSON definition
JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy to read and write, but also easy to parse and generate. It is based on a subset of the JavaScript programming language in the ECMA262 Language Specification (1999-12 Third edition). JSON uses text formats that are not related to programming languages, but also uses the idioms of the C language (c, C + +, C #, Java, JavaScript, Perl, Python, and so on) that make JSON an ideal data interchange format.

The structure of the JSON is based on the following two points

      • 1. "Name/value" pairs are set in different languages, it is understood as objects (object), records (record), structure (struct), Dictionary (dictionary), hash table (hash table), key list (keyed list), etc.
      • 2. Ordered list of values is understood in most languages array (array)

JSON uses :
JSON represents a JavaScript object in a specific string form. If you assign a string of this form to any JavaScript variable, the variable becomes an object reference, and this object is constructed from a string, which seems a bit awkward, and we use the example to illustrate it.

This assumes that we need to create a user object with the following properties

    • User ID
    • User name
    • User Email

      You can use the following JSON form to represent the user object:

    • {"UserID": One, "Name": "Truly", "email": "[email protected]"};

      Then if you assign this string to a JavaScript variable, you can use any of the properties of the object directly.

      Full code:

    • <script>var User = {"UserID": One, "Name": "Truly", "Email": "Zhuleipro hotmail.com"};alert ( User.Name); </script>

      The actual use may be more complicated, for example, we define a more detailed structure for name so that it has FirstName and LastName:

      {"UserID": One, "Name": {"FirstName": "Truly", "LastName": "Zhu"}, "Email": "Zhuleipro hotmail.com"}

      Full code:

    •  <script>var  User = {"UserID": One, "Name": {"FirstName": "Truly", " LastName ":" Zhu "}," Email ":" Zhuleipro hotmail.com "};alert (User.Name.FirstName);  </script> 

      Now that we're adding a new requirement, a page needs a list of users, not just a single user information, then you need to create an array of user lists.
      The following code demonstrates the use of JSON to define this user list:

    •  <script>var  userlist = [{  UserID: One, "Name": {"FirstName": "Truly", "LastName": "Zhu"}, "Email": "Zhuleipro hotmail.com" },{ "UserID": "Name": {"FirstName": "Jeffrey", "LastName": "Richter"}, "Email": "XXX xxx.com" },{ UserID: "Name": {"FirstName": "Scott", "LastName": "Gu"}, "Email": "Xxx2 xxx2.com" }];alert (userlist[ 0]. Name.firstname);  </script> 

      In fact, in addition to using the "." In addition to the reference attribute, we can also use the following statement:

      alert (userlist[0]["name" ["FirstName"]), or alert ( Userlist[0]. name["FirstName"]); 

      Readers should now be aware of the use of JSON, summed up in the following points:

      • An object is a collection of property, value pairs. An object begins with "{" and ends with "}". Each property name and value are ":" Prompts, separated by "," between the attributes.
      • An array is a collection of sequential values. An array starts at "[", Ends with "]", and the values are separated by ",".
      • The value can be a string in quotation marks, a number, true, false, NULL, or an object or an array. These structures can be nested.
      • The definition of strings and numbers is basically consistent with C or Java.

"Java" JSON format parsing

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.