package com.fh.test;
import java.io.IOException;
import java.util.ArrayList;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fh.panghu.entity.User;
import net.sf.json. JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;
/**
*
* @author fcbird
* With the help of the third-party jar package of net.sf.json The class completes the conversion between entity class and json object, entity class collection and jsonArray conversion
*
*/
public class TestJsonEntityListMap { @SuppressWarnings
("unchecked")
public static void getData() {
JSONObject objec1t = new JSONObject();
JSONObject objec2t = new JSONObject();
JSONArray jsonArray = new JSONArray();
ArrayList<User> users = new ArrayList<User>();
ArrayList<User> users12 = new ArrayList<User>();
JsonConfig jsonConfig = new JsonConfig();
ObjectMapper objectMapper=new ObjectMapper();
User user= new User();
User user12= new User();
//构造数据
user.setDdid("ddid");
user.setMobile("mobile");
user.setUserid("password");
user.setPassword("userid");
user.setUsername("username");
user12.setDdid("ddid1");
user12.setMobile("mobile1");
user12.setUserid("password1");
user12.setPassword("userid1");
user12.setUsername("username1");
objec1t.put("ddid", "ddid1");
objec1t.put("mobile", "mobile1");
objec1t.put("password", "password1");
objec1t.put("userid", "userid1");
objec1t.put("username", "username1");
objec2t.put("ddid", "ddid2");
objec2t.put("mobile", "mobile2");
objec2t.put("password", "password2");
objec2t.put("userid", "userid2");
objec2t.put("username", "username2");
users.add(user);
users.add(user12);
jsonArray.add(objec1t);
jsonArray.add(objec2t);
//Entities are converted to JSON objects
//The first
Strings of entities are converted to JSON objects jsonString1 = JSONSerializer.toJSON(user).toString( );
//Entities are converted to JSON objects. The second
String jsonString2 = JSONObject.fromObject(user).toString();
//JSON objects are converted to entities .
//The JSON objects are converted to entities. The first type of
User user1=(User)JSONObject .toBean(objec1t, User.class);
User user2 = new User();
try {
//The second JSON object is converted to an entity
user2=objectMapper.readValue(objec1t.toString(), User.class);
} catch ( IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//The entity class collection is converted to JSONArray
String jsonArrayString = JSONArray.fromObject(users, jsonConfig).toString();
users12 = (ArrayList<User>) JSONArray.toCollection (jsonArray,User.class);
System.out.println("The entity is converted to json1:"+jsonString1);
System.out.println("The entity is converted to json2:"+jsonString2);
System.out.println("json1 Convert to entity :"+user1);
System.out.println("json2 convert to entity:"+user2);
System.out.println("entity collection convert to jsonArray:"+jsonArrayString);
System.out.println( "jsonArray to entity collection:"+users12);
}
public static void main(String[] args) {
getData();
}
}
import java.io.IOException;
import java.util.ArrayList;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fh.panghu.entity.User;
import net.sf.json. JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;
/**
*
* @author fcbird
* With the help of the third-party jar package of net.sf.json The class completes the conversion between entity class and json object, entity class collection and jsonArray conversion
*
*/
public class TestJsonEntityListMap { @SuppressWarnings
("unchecked")
public static void getData() {
JSONObject objec1t = new JSONObject();
JSONObject objec2t = new JSONObject();
JSONArray jsonArray = new JSONArray();
ArrayList<User> users = new ArrayList<User>();
ArrayList<User> users12 = new ArrayList<User>();
JsonConfig jsonConfig = new JsonConfig();
ObjectMapper objectMapper=new ObjectMapper();
User user= new User();
User user12= new User();
//构造数据
user.setDdid("ddid");
user.setMobile("mobile");
user.setUserid("password");
user.setPassword("userid");
user.setUsername("username");
user12.setDdid("ddid1");
user12.setMobile("mobile1");
user12.setUserid("password1");
user12.setPassword("userid1");
user12.setUsername("username1");
objec1t.put("ddid", "ddid1");
objec1t.put("mobile", "mobile1");
objec1t.put("password", "password1");
objec1t.put("userid", "userid1");
objec1t.put("username", "username1");
objec2t.put("ddid", "ddid2");
objec2t.put("mobile", "mobile2");
objec2t.put("password", "password2");
objec2t.put("userid", "userid2");
objec2t.put("username", "username2");
users.add(user);
users.add(user12);
jsonArray.add(objec1t);
jsonArray.add(objec2t);
//Entities are converted to JSON objects
//The first
Strings of entities are converted to JSON objects jsonString1 = JSONSerializer.toJSON(user).toString( );
//Entities are converted to JSON objects. The second
String jsonString2 = JSONObject.fromObject(user).toString();
//JSON objects are converted to entities .
//The JSON objects are converted to entities. The first type of
User user1=(User)JSONObject .toBean(objec1t, User.class);
User user2 = new User();
try {
//The second JSON object is converted to an entity
user2=objectMapper.readValue(objec1t.toString(), User.class);
} catch ( IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//The entity class collection is converted to JSONArray
String jsonArrayString = JSONArray.fromObject(users, jsonConfig).toString();
users12 = (ArrayList<User>) JSONArray.toCollection (jsonArray,User.class);
System.out.println("The entity is converted to json1:"+jsonString1);
System.out.println("The entity is converted to json2:"+jsonString2);
System.out.println("json1 Convert to entity :"+user1);
System.out.println("json2 convert to entity:"+user2);
System.out.println("entity collection convert to jsonArray:"+jsonArrayString);
System.out.println( "jsonArray to entity collection:"+users12);
}
public static void main(String[] args) {
getData();
}
}
Screenshot of test results: