JSON Learning Notes

Source: Internet
Author: User

Time: 2017-1-1-22:25

--What is JSON

JSON (JavaScript Object Notation) is a lightweight data interchange format.
JSON is a string representation of a JavaScript object, such as a JSON-formatted string that can be sent to the client in a servlet, and JavaScript can execute the string to get a JavaScript object.

XML can also be used as a data interchange format.

--json Object Syntax

JSON syntax:
1) data in key-value pairs
2) data separated by commas
3) Save the object with curly braces
4) square brackets to save the array

JSON value:
1) number (integer or floating point)
2) string (in double quotes)
3) Logical value (TRUE or FALSE)
4) Array (in square brackets)
5) object (in curly braces)
6) NULL

Apply JSON:
var person = {' name ': ' Zhangsan ', ' age ': ' A ', ' sex ': ' Male '};
Alert (Person.name + "," + Person.age + "," + person.sex);

Note: Key also needs to be in double quotes.

Comparison of--json and XML

1. Readability: XML wins
2, decoding difficulty: JSON itself is the JS object (Pro-son), so much simpler
3, Popularity: XML has been popular for many years, but in the field of Ajax, JSON is more popular

--Convert a Java object to a JSON object

Apache provides a json-lib gadget that makes it easy to use the Java language to create JSON strings, or to convert JavaBean into strings.

1. Json-lib Core JAR Package
* Json-lib.jar

2, Json-lib dependent jar Package
* Commons-lang.jar
* Commons-beanutils.jar
* Commons-logging.jar
* Commons-collections.jar
* Ezmorph.jar

3, the core class in Json-lib
* Jsonobject
> itself is a map
> ToString () method to get a JSON string
> Jsonobject map = jsonobject.fromobject (person);
Convert Objects to Jsonobject objects

* Jsonarray
> itself is a list
> ToString () method to get a JSON string
> Jsonarray list = jsonarray.fromobject (list)
Convert list to Jsonarray object

=============================================================================

Code:

Import Java.util.arraylist;import java.util.List; Import Net.sf.json.jsonarray;import Net.sf.json.JSONObject; Import Org.junit.Test;        /** * Demo Json-lib Gadget * * @author WYC * */public class Demo {/* * as Map with */@Test public void fun1 () {        Jsonobject map = new Jsonobject ();        Map.put ("name", "Zhangsan");         Map.put ("Age", 20);    System.out.println (Map.tostring ()); }

Output:
{"Name": "Zhangsan", "Age": 20}
--------------------------------------------------------------------------------------------------------------- -------------/* * When a person object is already in place, you can convert person to Jsonobject object to use */@Test public void fun2 () {Perso         n p = new person ("Zhangsan", 21);        /* * Convert object to jsonobject type */jsonobject map = Jsonobject.fromobject (p);    SYSTEM.OUT.PRINTLN (map); }

Output:
{"Name": "Zhangsan", "Age": 21}
--------------------------------------------------------------------------------------------------------------- -------------/* * Jsonarray */@Test public void Fun3 () {person P1 = new Person ("Zhangsan", 22)        ;         person P2 = new Person ("Lisi", 23);        Jsonarray list = new Jsonarray ();        List.add (p1);        List.add (p2);    SYSTEM.OUT.PRINTLN (list); }

Output:
[{' name ': ' Zhangsan ', ' age ': 22},{' name ': ' Lisi ', ' age ': 23}]
--------------------------------------------------------------------------------------------------------------- -------------/* * There is a list that needs to convert the list to Jsonarray */@Test public void Fun4 () {person P1 = new P        Erson ("Zhangsan", 22);         person P2 = new Person ("Lisi", 23);        list<person> list = new arraylist<person> ();        List.add (p1);        List.add (p2);    System.out.println (Jsonarray.fromobject (list)); }}

Output:
[{' name ': ' Zhangsan ', ' age ': 22},{' name ': ' Lisi ', ' age ': 23}]

JSON Learning notes

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.