JSON Basics (Java)

Source: Internet
Author: User
Tags ming

1.json maven Dependency (take the first package for example)

<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20160212</version>
</dependency>

(

<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>

)

2.JSON String notation, denoted by an escape character:

String aa = "{\" 1\ ": \" Xiao red \ ", \" 2\ ": \" Xiao Wang \ ", \" 3\ ": \" Xiao Ming \ "," 4\ ": \" Xiao Zhang \ "}";

3.map conversion to JSON

Method One:

map<string, string> map = new hashmap<> ();
Map.put ("1", "Little Red");
Map.put ("2", "Xiao Wang");
Map.put ("3", "xiaoming");
Map.put ("4", "Xiao Zhang");
Map.put ("5", "Xiao Zhao");
Objectmapper json = new Objectmapper ();

try {
String string = json.writevalueasstring (map);
Jsonobject jsonobject = new Jsonobject (string);

} catch (Jsonprocessingexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

Method 2:

Introducing Fastjson jar packages with Maven

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.24</version>
</dependency>

New Hashmap<>();        Map.put ("1", "Little Red");        Map.put ("2", "Xiao Wang");        Map.put ("3", "Xiaoming");        Map.put ("4", "Xiao Zhang");        Map.put ("5", "Xiao Zhao");         = json.tojsonstring (map);        System.out.println (astring);

Analysis of 4.JSON

(1) The first Jar package parsing method:

String str = "{\" 1\ ": \" Little red \ ", \" 2\ ": \" Xiao Wang \ ", \" 3\ ": \" Xiao Ming \ "," 4\ ": \" Xiao Zhang \ "}";

Jsonobject jsonobject = new Jsonobject (str);

System.out.println (Jsonobject.getstring ("1"));
System.out.println (Jsonobject.getstring ("2"));

System.out.println (Jsonobject.getstring ("3"));

Multilayer JSON nesting:

String jsondata= "{\" name\ ": \" lily\ ", \" age\ ": 23,\" addr\ ": {\" city\ ": Guangzhou,\" province\ ": Guangdong}}";
Jsonobject jsonObject1 =new jsonobject (jsondata);
Jsonobject jsonObject2 = jsonobject1.getjsonobject ("addr");
System.out.println (jsonobject2.getstring ("City"));

(2) Second Jar package parsing method (requires [] to wrap the string)

String AA = "[{\" 1\ ": \" small red \ ", \" 2\ ": \" Xiao Wang \ ", \" 3\ ": \" Xiao Ming \ ", \" 4\ ": \" Xiao Zhang \ "}]";

Jsonarray Jsonarray = Jsonarray.fromobject (AA);
if (jsonarray.size () > 0) {
Iterate through the Jsonarray array and turn each object into a JSON object
for (int i = 0; i < jsonarray.size (); i++) {
Jsonobject Jsonobject = Jsonarray.getjsonobject (i);
System.out.println (Jsonobject.get ("1"));
System.out.println (Jsonobject.get ("2"));
System.out.println (Jsonobject.get ("3"));
}
}

5.JSON to convert to his format

(1) to String

String str = jsonobject.tostring ();

JSON Basics (Java)

Related Article

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.