阿里fastjson.jar使用
2015-11-28 16:19
609 查看
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 parseObject(String text, Class clazz); // 把JSON文本parse为JavaBean
public static final JSONArray parseArray(String text); //把JSON文本parse成JSONArray
public static final List parseArray(String text, Class 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
public static T parse2Bean(String jsonString, Class cls) {
T t = null;
//调用JSON类中的方法,parse为Bean对象
t = JSON.parseObject(jsonString, cls);
return t;
}
pubic class UserInfo implements Serializable{
private String name;
private int age;
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
public void setAge(int age){
this.age=age;
}
public int getAge(){
return age;
}
}
public class TestOne{
public static void main(String[] args){
UserInfo info=new UserInfo();
info.setName("zhangsan");
info.setAge(24);
//将对象转换为JSON字符串
String str_json=JSON.toJSONString(info);
System.out.println("JSON="+str_json);
}
}
public static void test2() {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("username", "zhangsan");
map.put("age", 24);
map.put("sex", "男");
//map集合
HashMap<String, Object> temp = new HashMap<String, Object>();
temp.put("name", "xiaohong");
temp.put("age", "23");
map.put("girlInfo", temp);
//list集合
List<String> list = new ArrayList<String>();
list.add("爬山");
list.add("骑车");
list.add("旅游");
map.put("hobby", list);
/*JSON 序列化,默认序列化出的JSON字符串中键值对是使用双引号,如果需要单引号的JSON字符串, [eg:String jsonString = JSON.toJSONString(map, SerializerFeature.UseSingleQuotes);]
*fastjson序列化时可以选择的SerializerFeature有十几个属性,你可以按照自己的需要去选择使用。
*/
String jsonString = JSON.toJSONString(map);
System.out.println("JSON=" + jsonString);
}
public void test3(){
String json="{\"name\":\"chenggang\",\"age\":24}";
//反序列化
UserInfo userInfo=JSON.parseObject(json,UserInfo.class);
System.out.println("name:"+userInfo.getName()+", age:"+userInfo.getAge());
}
/**泛型的反序列化*/
public static void test4(){
String json="{\"user\":{\"name\":\"zhangsan\",\"age\":25}}";
Map<String, UserInfoBean> map = JSON.parseObject(json, new TypeReference<Map<String, UserInfoBean>>(){});
System.out.println(map.get("user"));
}
public void test5(){
Date date=new Date();
//输出毫秒值
System.out.println(JSON.toJSONString(date));
//默认格式为yyyy-MM-dd HH:mm:ss
System.out.println(JSON.toJSONString(date, SerializerFeature.WriteDateUseDateFormat));
//根据自定义格式输出日期
System.out.println(JSON.toJSONStringWithDateFormat(date, "yyyy-MM-dd", SerializerFeature.WriteDateUseDateFormat));
}
Article articleOne = new Article();
JSONArray arrayOne = new JSONArray();
String attachmentJson = qqArticle.getStr("attachment_json");
JSONArray array = JSON.parseArray(attachmentJson);
object = (JSONObject) array.get(0);
arrayOne.add(object);
public static final JSONObject parseObject(String text); //把JSON文本parse成JSONObject
public static final T parseObject(String text, Class clazz); // 把JSON文本parse为JavaBean
public static final JSONArray parseArray(String text); //把JSON文本parse成JSONArray
public static final List parseArray(String text, Class 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
public static T parse2Bean(String jsonString, Class cls) {
T t = null;
//调用JSON类中的方法,parse为Bean对象
t = JSON.parseObject(jsonString, cls);
return t;
}
pubic class UserInfo implements Serializable{
private String name;
private int age;
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
public void setAge(int age){
this.age=age;
}
public int getAge(){
return age;
}
}
public class TestOne{
public static void main(String[] args){
UserInfo info=new UserInfo();
info.setName("zhangsan");
info.setAge(24);
//将对象转换为JSON字符串
String str_json=JSON.toJSONString(info);
System.out.println("JSON="+str_json);
}
}
public static void test2() {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("username", "zhangsan");
map.put("age", 24);
map.put("sex", "男");
//map集合
HashMap<String, Object> temp = new HashMap<String, Object>();
temp.put("name", "xiaohong");
temp.put("age", "23");
map.put("girlInfo", temp);
//list集合
List<String> list = new ArrayList<String>();
list.add("爬山");
list.add("骑车");
list.add("旅游");
map.put("hobby", list);
/*JSON 序列化,默认序列化出的JSON字符串中键值对是使用双引号,如果需要单引号的JSON字符串, [eg:String jsonString = JSON.toJSONString(map, SerializerFeature.UseSingleQuotes);]
*fastjson序列化时可以选择的SerializerFeature有十几个属性,你可以按照自己的需要去选择使用。
*/
String jsonString = JSON.toJSONString(map);
System.out.println("JSON=" + jsonString);
}
public void test3(){
String json="{\"name\":\"chenggang\",\"age\":24}";
//反序列化
UserInfo userInfo=JSON.parseObject(json,UserInfo.class);
System.out.println("name:"+userInfo.getName()+", age:"+userInfo.getAge());
}
/**泛型的反序列化*/
public static void test4(){
String json="{\"user\":{\"name\":\"zhangsan\",\"age\":25}}";
Map<String, UserInfoBean> map = JSON.parseObject(json, new TypeReference<Map<String, UserInfoBean>>(){});
System.out.println(map.get("user"));
}
public void test5(){
Date date=new Date();
//输出毫秒值
System.out.println(JSON.toJSONString(date));
//默认格式为yyyy-MM-dd HH:mm:ss
System.out.println(JSON.toJSONString(date, SerializerFeature.WriteDateUseDateFormat));
//根据自定义格式输出日期
System.out.println(JSON.toJSONStringWithDateFormat(date, "yyyy-MM-dd", SerializerFeature.WriteDateUseDateFormat));
}
Article articleOne = new Article();
JSONArray arrayOne = new JSONArray();
String attachmentJson = qqArticle.getStr("attachment_json");
JSONArray array = JSON.parseArray(attachmentJson);
object = (JSONObject) array.get(0);
arrayOne.add(object);
相关文章推荐
- javascript限制input输入框输入的内容
- js冒泡排序,选择排序,插入排序
- javascript利用正则获取第一根斜线后的内容
- javascript动态添加、删除按钮和input输入框
- 高性能javascript总结(2)
- ArcGIS的工作空间
- js取整数、取余数的方法
- js数据类型转换
- JS改变文字透明度
- 在ACRGIS Javascript中加入谷歌街道图和卫星图
- 高性能javascript总结
- [JSP]JSP到Servlet的转换
- JS 判断undefined
- location.hash详解
- Extjs各版本的下载链接
- JSON:在两个activity之间使用intent传递list类型的对象
- javascript加密之md5加密
- jsp页面中的代码执行加载顺序介绍
- js选项卡
- JavaScript数据类型和数据类型之间的转换关系