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