Jackson won praise for his excellent analytical performance, and today he has a look at some of Jackson's simple uses.
Jackson has to download before use, there are a total of three jar packages, to get the perfect Jackson experience, these three jar packages are indispensable. Java–>json
1. Output a class as a JSON string:
Outputs a class as a JSON string
@Test public
void Test1 () {
Objectmapper mapper = new Objectmapper ();
User user = new user ();
User.setmoney (1000);
User.setusername ("John");
User.setpassword ("123");
try {
System.out.println (mapper.writevalueasstring (user));
} catch (IOException e) {
E.printstacktrace ();
}
User.java
Import java.io.Serializable;
public class User implements serializable{
private String username;
private String password;
Fields that have been added to the transient property are not stored as
private int money;
public int Getmoney () {return money
;
}
public void Setmoney (int $) {
This.money = money;
}
Public user () {
} public
User (string Username, string password, int-money) {
this.username = username;
this.password = password;
This.money = money;
}
Public String GetUserName () {return
username;
}
public void Setusername (String username) {
this.username = username;
}
Public String GetPassword () {return
password;
}
public void SetPassword (String password) {
this.password = password;
}
Output:
2. Output a slightly more complex class in the form of a JSON string:
Book.java
public class Book {private int id;
private String name;
private int price;
Private String author;
Private Detail Detail;
private attribute attribute;
Public Attributes GetAttribute () {return attribute;
public void SetAttribute (attribute) {this.attribute = attribute;
Public Detail Getdetail () {return Detail;
public void Setdetail (Detail Detail) {this.detail = Detail;
public int getId () {return id;
The public void setId (int id) {this.id = ID;
Public String GetName () {return name;
public void SetName (String name) {this.name = name;
public int GetPrice () {return price;
public void Setprice (int price) {this.price = Price;
Public String Getauthor () {return author;
} public void Setauthor (String author) {this.author = author;
}
}
Detail.java
public class Detail {
private String presstime;
Private String Storytime;
Public String Getpresstime () {return
presstime;
}
public void Setpresstime (String presstime) {
this.presstime = presstime;
}
Public String Getstorytime () {return
storytime;
}
public void Setstorytime (String storytime) {
this.storytime = storytime;
}
}
Attribute.java
public class Attribute {
private String category;
Private String edition;
Public String GetCategory () {return
category;
}
public void Setcategory (String category) {
this.category = category;
}
Public String getedition () {return
edition;
}
public void Setedition (String edition) {
this.edition = edition;
}
}
The output is:
{"id": 1, "name": "The Chinese Kingdoms", "Price": "Author": "Luo Guan Zhong", "detail": {"presstime": "2001-01-01", "Storytime": "196-05-06"}, " Attribute ": {" category ":" Novel "," edition ":" 9 "}}
3. Output a list collection as a JSON string:
@Test public
void Test2 () {
Objectmapper mapper = new Objectmapper ();
list<user> list = new arraylist<user> ();
User U = New User ("John", "123", 1000);
List.add (u);
u = new User ("Dick", "456",);
List.add (u);
u = new User ("Harry", "789", 3000);
List.add (u);
u = new User ("Zhao Liu", "555", 4000);
List.add (u);
try {
System.out.println (mapper.writevalueasstring (list);
} catch (Jsonprocessingexception e) {
// TODO auto-generated Catch block
e.printstacktrace ();
}
Output results:
[{"username": "John", "Password": "123", "Money": 1000},{"username": "Dick", "Password": "456", "Money": 2000},{"username": " Harry "," Password ":" 789 "," Money ": 3000},{" username ":" Zhao Liu "," Password ":" 555 "," Money ": 4000}]
4. Output a map as a JSON string:
@Test public
void Test3 () {
Objectmapper mapper = new Objectmapper ();
map<string, string> map = new hashmap<string, string> ();
Map.put ("username", "John");
Map.put ("Password", "123456");
try {
System.out.println (mapper.writevalueasstring (map));
} catch (Jsonprocessingexception e) {
E.printstacktrace ();
}
The results are:
5. What if you want to export the map in the list collection in JSON string format. Same as the previous list.
@Test public
void Test4 () {
Objectmapper mapper = new Objectmapper ();
list<map<string, string>> list = new arraylist<map<string,string>> ();
map<string, string> map = new hashmap<string, string> ();
Map.put ("username", "John");
Map.put ("Password", "123456");
List.add (map);
Map = new hashmap<string, string> ();
Map.put ("username", "Dick");
Map.put ("Password", "888888");
List.add (map);
try {
System.out.println (mapper.writevalueasstring (list);
} catch (Jsonprocessingexception e) {
E.printstacktrace ();
}
6. In a look at a map there are other classes in the Book.java,book.java:
@Test public
void Test1 () {
Objectmapper mapper = new Objectmapper ();
Detail Detail = new Detail ();
Detail.setpresstime ("2001-01-01");
Detail.setstorytime ("196-05-06");
Attribute attr = new attribute ();
Attr.setcategory ("novel");
Attr.setedition ("9");
Book book = new book ();
Book.setattribute (attr);
Book.setauthor ("Luo Guan Zhong");
Book.setdetail (detail);
Book.setid (1);
Book.setname ("The Kingdoms");
Book.setprice (a);
map<string, object> map = new hashmap<string, object> ();
Map.put ("namespace", "books");
Map.put ("book", book);
try {
System.out.println (mapper.writevalueasstring (map));
} catch (IOException e) {
E.printstacktrace ();
}
Output results:
{"book": {"id": 1, "name": "The Chinese Kingdoms", "Price": "Author": "Luo Guan Zhong", "detail": {"presstime": "2001-01-01", "Storytime": " 196-05-06 "}," attribute ": {" category ":" Novel "," edition ":" 9 "}}," namespace ":" Books "}
Json–>java
1.json string converted to JavaBean:
@Test public
void Test5 () {
String str = "{\ id\": 1,\ "name\": \ "the Chinese kingdoms \", \ "price\": 20,\ "author\": \ "Luo Guan," "," Detail\ ": {\" presstime\ ": \" 2001-01-01\ ", \" storytime\ ": \" 196-05-06\ "},\" attribute\ ": {\" category\ ": \" novel \ ", \" edition\ ": \" 9\ "}}";
Objectmapper mapper = new Objectmapper ();
try {book Book
= Mapper.readvalue (str, book.class);
System.out.println (Book.getauthor () + "," +book.getattribute (). GetCategory ());
catch (IOException e) {
e.printstacktrace ();
}
}
2.json string to List
Json-->list
@Test public
void Test6 () {
String str = "[{\ username\": \ "john \", \ "password\": \ "123\", \ " Money\ ": 1000},{\" username\ ": \" Dick \ ", \" password\ ": \" 456\ ", \" money\ ": 2000},{\" username\ ": \" Harry "," password\ ": \" 789\ ", \" money\ ": 3000},{\" username\ ": \" Zhao Liu ", \" password\ ": \" 555\ ", \" money\ ": 4000}]";
Objectmapper mapper = new Objectmapper ();
try {
list<user> US = mapper.readvalue (str, new typereference<arraylist<user>> () {});
for (User user:us) {
System.out.println (user.getusername () + ", +user.getmoney ());
}
catch ( IOException e) {
e.printstacktrace ();
}
}
The 3.json string is converted to map:
Json-->map
@Test public
void Test7 () {
String str = "{\ password\": \ "888888\", \ "username\": \ "Dick \"} " ;
Objectmapper mapper = new Objectmapper ();
try {
map<string, string> Map = Mapper.readvalue (str, new typereference<map<string, string>> () {} );
For (String Key:map.keySet ()) {
System.out.println (key+ ", +map.get (key));
}
catch (IOException e) { C11/>e.printstacktrace ();
}
Alas, carefully pondering, this Jackson really good simple, had always thought it was difficult, think of the text of the primary school "Pony River", it seems to be more practice.