Net.sf.json and Fastjson Two kinds of jar package use

Source: Internet
Author: User

Let's be clear: these two ways are two different ways of JSON parsing, either.

First, the introduction of Net.sf.json package

First use Net.sf.json package, of course you have to import a lot of packages to support Commons-beanutils-1.7.0.jar Commons-collections-3.1.jar Commons-lang-2.5.jar Commons-logging.jar Ezmorph-1.0.3.jar Json-lib-2.1-jdk15.jar, self-Baidu download it

Then use the method:

JSON string: {\ "code\": 0,\ "msg\": \ "success \", \ "data\": {\ "template\": {\ "templateid\": 23,\ "code\": \ "redmine\", \ "type\": 0,\ " Status\ ": 0,\" nodename\ ": \" membership expires soon ", \" detail\ ": \" 1 months \ ", \" content\ ": \" your member ... \ "}}

Call:

map<string,string> dto = new hashmap<string,string> ();
Dto.put ("Code", App.getcode ());

Xxservice.getxx (Jsonobject.fromobject (DTO). toString ());

Analytical:

Jsonobject jsonobject = Jsonobject.fromobject (msgcontent);
Jsonobject O2=jsonobject.fromobject (Jsonobject.get ("Data"));
Jsonobject O3=jsonobject.fromobject (O2.get ("template"));

(String) O3.get ("content")

Well, you get the bottom of the story.

If it is used in the JSON string [] this will need to useJSONArray了,这篇博文写的不错http://blog.csdn.net/qq_22792489/article/details/51111890

Sometimes you just change the original base on the code, but others use Com.alibaba.fastjson package, no way, can only be compatible with Bai, then use Com.alibaba.fastjson bar, but the method name is not the same, the function of the implementation of the line

Ii. Introduction of Com.alibaba.fastjson Package

Call:

map<string,string> dto = new hashmap<string,string> ();
Dto.put ("code", "haha");
System.out.println (Jsonobject.tojson (DTO). toString ());

Analytical:

Jsonobject Jsonobject = jsonobject.parseobject (result);
Jsonobject O2=jsonobject.parseobject (String) jsonobject.getstring ("Data");
Jsonobject O3=jsonobject.parseobject (String) o2.getstring ("template");
System.out.println (String) jsonobject.getstring ("Data");
System.out.println (String) o2.getstring ("template");
System.out.println (String) o3.getstring ("content");

The bottom of the template content also has, is done!

Net.sf.json and Fastjson Two kinds of jar package use

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.