您的位置:首页 > Web前端 > JavaScript

阿里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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: