[转载]FastJson 示例 及问题 - 心lu历程 - ITeye技术网站

[转载]FastJson 示例 及问题 – 心lu历程 – ITeye技术网站.

不能以内部类的形式,使用parSEObject方法,会报:com.alibaba.fastjson.JSONException: create instance error

===========================================================================

http://www.cnblogs.com/zhenmingliu/archive/2011/12/29/2305775.html
http://871421448.iteye.com/blog/1581876
http://blog.sina.com.cn/s/blog_4f925fc30102egx0.html

有这个,解析应该不是问题了

package com.shihuan.test.myfastjson.vo;

import java.util.Date;

public class MyUser {

private Long userid;
private String username;
private boolean sex;
private Date birthday;

public MyUser() {
}

public Long getUserid() {
return userid;
}

public void setUserid(Long userid) {
this.userid = userid;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public boolean isSex() {
return sex;
}

public void setSex(boolean sex) {
this.sex = sex;
}

public Date getBirthday() {
return birthday;
}

public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}

MyGroupUser.java代码如下:
package com.shihuan.test.myfastjson.vo;

import java.util.ArrayList;
import java.util.List;

public class MyGroupUser {

private Long groupid;
private String groupname;
private List<MyUser> listuser = new ArrayList<MyUser>();

public MyGroupUser() {
}

public Long getGroupid() {
return groupid;
}

public void setGroupid(Long groupid) {
this.groupid = groupid;
}

public String getGroupname() {
return groupname;
}

public void setGroupname(String groupname) {
this.groupname = groupname;
}

public List<MyUser> getListuser() {
return listuser;
}

public void setListuser(List<MyUser> listuser) {
this.listuser = listuser;
}
}

MyGroupSetUser.java代码如下:
package com.shihuan.test.myfastjson.vo;

import java.util.HashSet;
import java.util.Set;

public class MyGroupSetUser {

private Long groupsetid;
private String groupsetname;
private Set<MyUser> setuser = new HashSet<MyUser>();

public MyGroupSetUser() {
}

public Long getGroupsetid() {
return groupsetid;
}

public void setGroupsetid(Long groupsetid) {
this.groupsetid = groupsetid;
}

public String getGroupsetname() {
return groupsetname;
}

public void setGroupsetname(String groupsetname) {
this.groupsetname = groupsetname;
}

public Set<MyUser> getSetuser() {
return setuser;
}

public void setSetuser(Set<MyUser> setuser) {
this.setuser = setuser;
}
}

MyGroupMapUser.java代码如下:
package com.shihuan.test.myfastjson.vo;

import java.util.HashMap;
import java.util.Map;

public class MyGroupMapUser {

private Long groupmapid;
private String groupmapname;
private Map<String, MyUser> mapuser = new HashMap<String, MyUser>();

public MyGroupMapUser() {
}

public Long getGroupmapid() {
return groupmapid;
}

public void setGroupmapid(Long groupmapid) {
this.groupmapid = groupmapid;
}

public String getGroupmapname() {
return groupmapname;
}

public void setGroupmapname(String groupmapname) {
this.groupmapname = groupmapname;
}

public Map<String, MyUser> getMapuser() {
return mapuser;
}

public void setMapuser(Map<String, MyUser> mapuser) {
this.mapuser = mapuser;
}
}

MyTestJson.java代码如下:
package com.shihuan.test.myfastjson.mytest;

import com.shihuan.core.framework.utils.DateHelper;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.parser.Feature;

import com.shihuan.test.myfastjson.vo.MyGroupMapUser;
import com.shihuan.test.myfastjson.vo.MyGroupSetUser;
import com.shihuan.test.myfastjson.vo.MyGroupUser;
import com.shihuan.test.myfastjson.vo.MyUser;

public class MyTestJson {

public static void main(String[] args) {
MyUser myUser = new MyUser();
myUser.setUserid(1L);
myUser.setUsername("shihuan");
myUser.setSex(true);
myUser.setBirthday(new Date());
String myuserjson = JSON.toJSONString(myUser, SerializerFeature.WriteDateUseDateFormat);
System.out.println(myuserjson);
String myuserjsondateformat = JSON.toJSONStringWithDateFormat(myUser, "yyyy-MM-dd", SerializerFeature.WriteDateUseDateFormat);
System.out.println(myuserjsondateformat);
String myuserjsondateformathm = JSON.toJSONStringWithDateFormat(myUser, "yyyy-MM-dd HH:mm:ss.SSS", SerializerFeature.WriteDateUseDateFormat);
System.out.println(myuserjsondateformathm);
MyUser myJsonUser = JSON.parseObject(myuserjson, MyUser.class);
System.out.println(myJsonUser.getUserid() + " -- " + myJsonUser.getUsername() + " -- " + myJsonUser.getBirthday() + " -- " + myJsonUser.isSex());
MyUser myJsonUserDateformat = JSON.parseObject(myuserjson, MyUser.class, Feature.AllowISO8601DateFormat);
System.out.println(myJsonUserDateformat.getUserid() + " -- " + myJsonUserDateformat.getUsername() + " -- " + DateHelper.getFormatString(myJsonUserDateformat.getBirthday()) + " -- " + myJsonUserDateformat.isSex());
System.out.println("-------------------------------------------------------------------------------------------------------------------------------------");
MyUser myUser1 = new MyUser();
myUser1.setUserid(1L);
myUser1.setUsername("shihuan1");
myUser1.setSex(true);
myUser1.setBirthday(new Date());
MyUser myUser2 = new MyUser();
myUser2.setUserid(2L);
myUser2.setUsername("shihuan2");
myUser2.setSex(false);
myUser2.setBirthday(new Date());
List<MyUser> listmyusers = new ArrayList<MyUser>();
listmyusers.add(myUser1);
listmyusers.add(myUser2);
//List -> JSON array
String listmyusersjson= JSON.toJSONString(listmyusers, false);   //第二个参数表示是否格式化输出
        System.out.println(listmyusersjson);
        //JSON array -> List
        List<MyUser> myUserList = JSON.parseArray(listmyusersjson, MyUser.class);
        for (MyUser myUserTmp : myUserList) {
        System.out.println(myUserTmp.getUserid() + " -- " + myUserTmp.getUsername() + " -- " + myUserTmp.isSex() + " -- " + myUserTmp.getBirthday());
        }
        System.out.println("-------------------------------------------------------------------------------------------------------------------------------------");
        System.out.println("复合List对象的VO对象开始......");
MyGroupUser myGroupUser = new MyGroupUser();
myGroupUser.setGroupid(1L);
myGroupUser.setGroupname("gshihuan");
myGroupUser.setListuser(listmyusers);
String mygroupuserjson = JSON.toJSONString(myGroupUser, SerializerFeature.WriteDateUseDateFormat);
System.out.println(mygroupuserjson);
MyGroupUser myJsonGroupUser = JSON.parseObject(mygroupuserjson, MyGroupUser.class);
System.out.println(myJsonGroupUser.getGroupid() + " -- " + myJsonGroupUser.getGroupname() + " -- " + myJsonGroupUser.getListuser().get(0).getUserid() + "&" + myJsonGroupUser.getListuser().get(0).getUsername() + "&" + myJsonGroupUser.getListuser().get(0).getBirthday() + "&" + myJsonGroupUser.getListuser().get(0).isSex() + " -- " + myJsonGroupUser.getListuser().get(1).getUserid() + "&" + myJsonGroupUser.getListuser().get(1).getUsername() + "&" + myJsonGroupUser.getListuser().get(1).getBirthday() + "&" + myJsonGroupUser.getListuser().get(1).isSex());
System.out.println("-------------------------------------------------------------------------------------------------------------------------------------");
System.out.println("复合Set对象的VO对象开始......");
Set<MyUser> setObjData = new HashSet<MyUser>();
setObjData.add(myUser1);
setObjData.add(myUser2);
MyGroupSetUser myGroupSetUser = new MyGroupSetUser();
myGroupSetUser.setGroupsetid(1L);
myGroupSetUser.setGroupsetname("gsetshihuan");
myGroupSetUser.setSetuser(setObjData);
String mygroupsetuserjson = JSON.toJSONString(myGroupSetUser, SerializerFeature.WriteDateUseDateFormat);
System.out.println(mygroupsetuserjson);
MyGroupSetUser myJsonGroupSetUser = JSON.parseObject(mygroupsetuserjson, MyGroupSetUser.class);
System.out.println(myJsonGroupSetUser.getGroupsetid() + " -- " + myJsonGroupSetUser.getGroupsetname());
for(Iterator<MyUser> myUserIter = myJsonGroupSetUser.getSetuser().iterator(); myUserIter.hasNext();){
MyUser nbUserObj = myUserIter.next();
System.out.println(nbUserObj.getUserid() + "&" + nbUserObj.getUsername() + "&" + nbUserObj.isSex() + "&" + nbUserObj.getBirthday());
}
System.out.println("-------------------------------------------------------------------------------------------------------------------------------------");
System.out.println("复合Map对象的VO对象开始......");
Map<String, MyUser> myUserMap = new HashMap<String, MyUser>();
myUserMap.put("1", myUser1);
myUserMap.put("2", myUser2);
MyGroupMapUser myGroupMapUser = new MyGroupMapUser();
myGroupMapUser.setGroupmapid(1L);
myGroupMapUser.setGroupmapname("gmapshihuan");
myGroupMapUser.setMapuser(myUserMap);
String mygroupmapuserjson = JSON.toJSONString(myGroupMapUser, SerializerFeature.WriteDateUseDateFormat);
System.out.println(mygroupmapuserjson);
MyGroupMapUser myJsonGroupMapUser = JSON.parseObject(mygroupmapuserjson, MyGroupMapUser.class);
System.out.println(myJsonGroupMapUser.getGroupmapid() + " -- " + myJsonGroupMapUser.getGroupmapname());
Map<String, MyUser> myUserMapData = myJsonGroupMapUser.getMapuser();
for(Object obj : myUserMapData.keySet()) { 
   String key = (String)obj; 
   MyUser value = (MyUser)myUserMapData.get(key); 
   System.out.println(key + " : " + value.getUserid() + "&" + value.getUsername() + "&" + value.isSex() + "&" + value.getBirthday()); 
}
for(Entry<String, MyUser> userMapData : myJsonGroupMapUser.getMapuser().entrySet()) { 
   Entry entry = (Entry)userMapData; 
   String key = (String) entry.getKey(); 
   MyUser value = (MyUser) entry.getValue(); 
   System.out.println(key + " : " + value.getUserid() + "&" + value.getUsername() + "&" + value.isSex() + "&" + value.getBirthday()); 
}
System.out.println("-------------------------------------------------------------------------------------------------------------------------------------");
//Map -> JSON
        Map<String, MyUser> map = new HashMap<String, MyUser>();
        map.put("a", myUser1);
        map.put("b", myUser2);
        String mapjson = JSON.toJSONString(map, false);
        System.out.println(mapjson);
        //JSON -> Map
        Map<String, MyUser> map1 = (Map<String, MyUser>)JSON.parse(mapjson);
        for (String key : map1.keySet()) {
        System.out.println(key + " : " + map1.get(key));
        }
       
        System.out.println("-------------------------------------------------------------------------------------------------------------------------------------");
       
        String[] arrStrs = {"a", "b", "c"};
        String jsonarrStrs = JSON.toJSONString(arrStrs, false);
        System.out.println(jsonarrStrs);
        JSONArray jsonArray1 = JSON.parseArray(jsonarrStrs);
        for (Object o : jsonArray1) {
        System.out.println(o);
        }
        System.out.println(jsonArray1);
       
        System.out.println("-------------------------------------------------------------------------------------------------------------------------------------");
       
        MyUser[] arrMyUsers = {myUser1, myUser2};
        String jsonarrMyUsers = JSON.toJSONString(arrMyUsers, false);
        System.out.println(jsonarrMyUsers);
        JSONArray jsonArray2 = JSON.parseArray(jsonarrMyUsers);
        for (Object o : jsonArray2) {
        System.out.println(o);
        }
        System.out.println(jsonArray2);
       
        System.out.println("-------------------------------------------------------------------------------------------------------------------------------------");
       
        Map zzmap = new HashMap();
        zzmap.put("a", "aaa");
        zzmap.put("b", "bbb");
        zzmap.put("c", "ccc");
        String zzmapjson = JSON.toJSONString(zzmap);
        System.out.println(zzmapjson);
        Map zzmapobj = JSON.parseObject(zzmapjson);
        for (Object o : zzmap.entrySet()) {
        Map.Entry<String, String> entry = (Map.Entry<String, String>)o;
            System.out.println(entry.getKey() + "--->" + entry.getValue());
        }
}

}

运行结果如下:
{"birthday":"2012-12-08 13:34:29","sex":true,"userid":1,"username":"shihuan"}
{"birthday":"2012-12-08","sex":true,"userid":1,"username":"shihuan"}
{"birthday":"2012-12-08 13:34:29.921","sex":true,"userid":1,"username":"shihuan"}
1 -- shihuan -- Sat Dec 08 13:34:29 CST 2012 -- true
1 -- shihuan -- 2012-12-08 -- true
-------------------------------------------------------------------------------------------------------------------------------------
[{"birthday":1354944870421,"sex":true,"userid":1,"username":"shihuan1"},{"birthday":1354944870421,"sex":false,"userid":2,"username":"shihuan2"}]
1 -- shihuan1 -- true -- Sat Dec 08 13:34:30 CST 2012
2 -- shihuan2 -- false -- Sat Dec 08 13:34:30 CST 2012
-------------------------------------------------------------------------------------------------------------------------------------
复合List对象的VO对象开始......
{"groupid":1,"groupname":"gshihuan","listuser":[{"birthday":"2012-12-08 13:34:30","sex":true,"userid":1,"username":"shihuan1"},{"birthday":"2012-12-08 13:34:30","sex":false,"userid":2,"username":"shihuan2"}]}
1 -- gshihuan -- 1&shihuan1&Sat Dec 08 13:34:30 CST 2012&true -- 2&shihuan2&Sat Dec 08 13:34:30 CST 2012&false
-------------------------------------------------------------------------------------------------------------------------------------
复合Set对象的VO对象开始......
{"groupsetid":1,"groupsetname":"gsetshihuan","setuser":[{"birthday":"2012-12-08 13:34:30","sex":true,"userid":1,"username":"shihuan1"},{"birthday":"2012-12-08 13:34:30","sex":false,"userid":2,"username":"shihuan2"}]}
1 -- gsetshihuan
2&shihuan2&false&Sat Dec 08 13:34:30 CST 2012
1&shihuan1&true&Sat Dec 08 13:34:30 CST 2012
-------------------------------------------------------------------------------------------------------------------------------------
复合Map对象的VO对象开始......
{"groupmapid":1,"groupmapname":"gmapshihuan","mapuser":{"1":{"birthday":"2012-12-08 13:34:30","sex":true,"userid":1,"username":"shihuan1"},"2":{"birthday":"2012-12-08 13:34:30","sex":false,"userid":2,"username":"shihuan2"}}}
1 -- gmapshihuan
2 : 2&shihuan2&false&Sat Dec 08 13:34:30 CST 2012
1 : 1&shihuan1&true&Sat Dec 08 13:34:30 CST 2012
2 : 2&shihuan2&false&Sat Dec 08 13:34:30 CST 2012
1 : 1&shihuan1&true&Sat Dec 08 13:34:30 CST 2012
-------------------------------------------------------------------------------------------------------------------------------------
{"a":{"birthday":1354944870421,"sex":true,"userid":1,"username":"shihuan1"},"b":{"birthday":1354944870421,"sex":false,"userid":2,"username":"shihuan2"}}
b : {"birthday":1354944870421,"sex":false,"userid":2,"username":"shihuan2"}
a : {"birthday":1354944870421,"sex":true,"userid":1,"username":"shihuan1"}
-------------------------------------------------------------------------------------------------------------------------------------
["a","b","c"]
a
b
c
["a","b","c"]
-------------------------------------------------------------------------------------------------------------------------------------
[{"birthday":1354944870421,"sex":true,"userid":1,"username":"shihuan1"},{"birthday":1354944870421,"sex":false,"userid":2,"username":"shihuan2"}]
{"birthday":1354944870421,"sex":true,"userid":1,"username":"shihuan1"}
{"birthday":1354944870421,"sex":false,"userid":2,"username":"shihuan2"}
[{"birthday":1354944870421,"sex":true,"userid":1,"username":"shihuan1"},{"birthday":1354944870421,"sex":false,"userid":2,"username":"shihuan2"}]
-------------------------------------------------------------------------------------------------------------------------------------
{"a":"aaa","b":"bbb","c":"ccc"}
b--->bbb
c--->ccc
a--->aaa
赞(0) 打赏
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏