JSON (JavaScript Object notation) is a lightweight data interchange format. Commonly used with the foreground interaction, in the use of Java and JSON will sometimes encounter the conversion between the situation, as follows:
Note To import the related JSON package before using
Define entity classes:
/**
* Inner class
* @author Administrator
*
*/
public static Class test{
private String name;
Private String word;
private String color;
Private String image;
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
Public String Getword () {
return word;
}
public void Setword (String word) {
This.word = Word;
}
Public String GetColor () {
return color;
}
public void SetColor (String color) {
This.color = color;
}
Public String GetImage () {
return image;
}
public void SetImage (String image) {
This.image = image;
}
}
/**
* JSON turn Java class
* @param String Json
* @throws IOException
* @throws jsonmappingexception
* @throws jsonparseexception
*/
Public Test Jsontoclass (String json) throws Jsonparseexception, Jsonmappingexception, ioexception{
Jsonobject Jsonobj=jsonobject.fromobject (JSON);
SYSTEM.OUT.PRINTLN ("JSON gets object:" +jsonobj.get ("name"));
Both of these methods are available
Test test= (Test) Jsonobject.tobean (Jsonobj, Test.class);
Test Test=mapper.readvalue (JSON, test.class);
return test;
}
/**
* Java class turn JSON
* @param class
*/
Public String Classtojson (Object test) {
Both of these methods are available
return Jsonserializer.tojson (Test). ToString ();
return Jsonobject.fromobject (Test). ToString ();
}
The conversion of list and JSON can refer to the above methods, divide the list into entities for conversion, and refer to the related integrated class packages.