1. Use Fastjson to convert string to map:
String out;
Object succesresponse = Json.parse (out); Convert to Object first
Map map = (map) succesresponse; Object strong convert to map
2. String to Java Object
Fastjson to convert a string string to a Java object or an array of objects
The code is as follows
Import java.util.ArrayList;
Import Java.util.Arrays;
Import java.util.List;
Import Com.alibaba.fastjson.JSON;
Import Com.alibaba.fastjson.JSONObject;
Import com.alibaba.fastjson.TypeReference; public class Testfastjson {public static void main (string[] args) {//Convert to Object String JSON
string = "{\" a\ ": 51,\" B\ ": 0}";
Usa u1 = Json.parseobject (jsonstring, New typereference<usa> () {});
Usa U2 = Json.parseobject (Jsonstring,usa.class);
Convert to object array String jsonstring2 = "[{\ A\": 51,\ "B\": 0}] ";
usa[] Usa2 = Json.parseobject (jsonstring2, New typereference<usa[]> () {});
List List = Arrays.aslist (USA2); Convert to ArrayList arraylist<usa> list2 = Json.parseobject (jsonstring2, New TYPEREFERENCE<ARRAYLIST<USA&G
T;> () {}); Convert to ArrayList (default) List3 the same as List4 effect arraylist<jsonobject> list3 = Json.parseobject (jsonstring2, New Arraylist<usa> (). GetClass ());
arraylist<jsonobject> list4 = Json.parseobject (jsonstring2, Arraylist.class);
for (int i = 0; i < list4.size (); i++) {//recommend this jsonobject IO = list4.get (i);
System.out.println (Io.get ("a") + "======adn=====" +io.get ("B"));
Class Usa {private int count = 1888;
Private String base = "Project";
Private Long A;
Public Long Geta () {return A;
public void SetA (Long a) {this.a = A;
Private String B;
Public String Getb () {return b;
public void Setb (String b) {this.b = b;
}
}
The JSON string is converted directly to the list
or convert to an array of objects
usa[] Usa2 = Json.parseobject (jsonstring2, New typereference<usa[]> () {});
Object array Goto List
List List = Arrays.aslist (USA2);
When we use new typereference, we generate multiple class files. How many new typereference will be added to class
Even if we write new typereference in the For Loop (0-n), the code generates a class file, Fastjson How many new typereference we've written, not how many times we've called New typereference. Recommended Use Arraylist.class