gson 数据转换json
另外还有阿里巴巴的fastjson也不错
Fastjson API入口类是com.alibaba.fastjson.JSON,常用的序列化操作都可以在JSON类上的静态方法直接完成。
public static final Object parse(String text); // 把JSON文本parse为JSONObject或者JSONArray
public static final JSONObject parseObject(String text); // 把JSON文本parse成JSONObject
public static final <T> T parseObject(String text, Class<T> clazz); // 把JSON文本parse为JavaBean
public static final JSONArray parseArray(String text); // 把JSON文本parse成JSONArray
public static final <T> List<T> parseArray(String text, Class<T> clazz); //把JSON文本parse成JavaBean集合
public static final String toJSONString(Object object); // 将JavaBean序列化为JSON文本
public static final String toJSONString(Object object, boolean prettyFormat); // 将JavaBean序列化为带格式的JSON文本
public static final Object toJSON(Object javaObject); 将JavaBean转换为JSONObject或者JSONArray。
这里就不联系demo了
[url]http://blog.csdn.net/wilsonke/article/details/37921571[/url]
package com.data.vo;
import com.google.gson.annotations.Expose;
public class GsonUser {
@Expose
private Integer gson_id;
private String gson_name;
public Integer getGson_id() {
return gson_id;
}
public void setGson_id(Integer gson_id) {
this.gson_id = gson_id;
}
public String getGson_name() {
return gson_name;
}
public void setGson_name(String gson_name) {
this.gson_name = gson_name;
}
public GsonUser(Integer gson_id, String gson_name) {
super();
this.gson_id = gson_id;
this.gson_name = gson_name;
}
public GsonUser() {
super();
}
}
package com.data.test;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import com.data.vo.GsonUser;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class Commontest {
private Gson gson = null;
private GsonBuilder gsonBuilder = null;
String gsonToString = null;
GsonUser gsonUser1 = new GsonUser(1,"美女");
GsonUser gsonUser2 = new GsonUser(2,"帅哥");
@Before
public void setup(){
gson = new Gson();
gsonBuilder = new GsonBuilder();
}
//@Test bean
public void testGson(){
GsonUser gsonUser = new GsonUser();
gsonUser.setGson_id(1);
gsonUser.setGson_name("美女");
GsonBuilder builder = new GsonBuilder();
// 不转换没有 @Expose 注解的字段
//builder.excludeFieldsWithoutExposeAnnotation();
Gson gson =builder.create();
String user = gson.toJson(gsonUser);
System.out.println(user);//{"gson_id":1,"gson_name":"美女"}
// 将JSON字符串转换为 JavaBean
GsonUser user2 = gson.fromJson(user, GsonUser.class);
System.out.println(user2.getGson_id()+","+user2.getGson_name());
}
//@Test
public void testList(){//list
List<Object> list = new ArrayList<Object>();
list.add(gsonUser1);list.add(gsonUser2);
gsonToString= gson.toJson(list);
//System.out.println(gsonToString);
//TypeToken 通过反射 type import java.lang.reflect.Type; 将泛型对象类型转成我们所需要的对象类型 //import com.google.common.reflect.TypeToken;
Type type = new TypeToken<List<GsonUser>>() {}.getType();//list 对象必须指定一个实体类。
List<GsonUser> list1 = gson.fromJson(gsonToString.toString(), type);
for (int i = 0; i < list1.size(); i++) {
GsonUser gs = (GsonUser) list1.get(i);
System.out.println(gs.getGson_id()+","+gs.getGson_name());
}
}
@Test
public void testMap(){//map
Map<String, GsonUser> userMap = new HashMap<String, GsonUser>();
userMap.put("gs", gsonUser1);userMap.put("ps1", gsonUser2);
String gsonToString =gson.toJson(userMap);
System.out.println(gsonToString);
Type type = new TypeToken<Map<String,GsonUser>>() {}.getType();
Map<String, GsonUser> map = gson.fromJson(gsonToString, type);
GsonUser u = map.get("gs");
System.out.println(u.getGson_id()+","+u.getGson_name());
}
}
另外还有阿里巴巴的fastjson也不错
Fastjson API入口类是com.alibaba.fastjson.JSON,常用的序列化操作都可以在JSON类上的静态方法直接完成。
public static final Object parse(String text); // 把JSON文本parse为JSONObject或者JSONArray
public static final JSONObject parseObject(String text); // 把JSON文本parse成JSONObject
public static final <T> T parseObject(String text, Class<T> clazz); // 把JSON文本parse为JavaBean
public static final JSONArray parseArray(String text); // 把JSON文本parse成JSONArray
public static final <T> List<T> parseArray(String text, Class<T> clazz); //把JSON文本parse成JavaBean集合
public static final String toJSONString(Object object); // 将JavaBean序列化为JSON文本
public static final String toJSONString(Object object, boolean prettyFormat); // 将JavaBean序列化为带格式的JSON文本
public static final Object toJSON(Object javaObject); 将JavaBean转换为JSONObject或者JSONArray。
这里就不联系demo了
[url]http://blog.csdn.net/wilsonke/article/details/37921571[/url]