`

FastJson 示例 及问题

 
阅读更多
不能以内部类的形式,使用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
有这个,解析应该不是问题了
MyUser.java代码如下:
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


========================================================
分享到:
评论
1 楼 Z_ying 2015-11-04  
谢谢分享,上次也遇到过的,解决了,过了一段时间又忘记,幸亏看到了你的文章,   还是要记下来

相关推荐

    fastjson的一个小例子

    **FastJson:一个高效、强大的Java JSON库** FastJson是阿里巴巴开源的一款高性能的JSON库,它主要用于Java对象与JSON字符串之间的转换。在这个小例子中,我们将深入探讨FastJson的使用方法,以及它如何帮助开发者...

    Android FastJSON小示例

    在Android开发中,数据交换和序列化是常见的需求,FastJSON是阿里巴巴提供的一款高效...通过以上示例,你应该对如何在Android Studio中使用FastJSON有了初步了解。在实践中不断探索,你会发现更多FastJSON的便捷之处。

    fastjson使用实例

    Fastjson是阿里巴巴开发的一款高效、功能强大的JSON库,它在Java世界中被广泛应用,尤其在大规模服务器...在实际项目中,结合提供的API和示例代码(如`FastJsonDemo`),开发者可以快速上手并熟练掌握Fastjson的使用。

    fastjson 示例

    在这个示例中,我们将深入探讨Fastjson的核心功能、使用方法以及在实际项目中的应用。 首先,Fastjson 的主要功能包括 JSON 字符串与 Java 对象之间的相互转换。通过 `parseObject()` 方法,可以将 JSON 字符串解析...

    SpringMVC+fastjson+Swagger集成示例源码

    这个示例源码对于学习和实践SpringMVC、Fastjson和Swagger的集成非常有帮助,它展示了如何在实际项目中有效地利用这些工具,提升开发效率和API的质量。开发者可以通过阅读和运行代码,更深入地理解这三个组件的协同...

    fastjson-1.2.47及源码、简单示例

    《深入解析Fastjson:1.2.47版本与源码分析及简单示例》 在Java开发中,处理JSON数据是一项常见的任务。Fastjson,作为阿里巴巴开源的一个高性能的JSON库,因其简单易用和高效的特性,深受广大开发者喜爱。本文将...

    fastjson1.2.2.jar

    三、Fastjson使用示例 1. **JSON字符串转Java对象** ```java String jsonString = "{\"name\":\"张三\",\"age\":25}"; Person person = JSON.parseObject(jsonString, Person.class); ``` 2. **Java对象转JSON...

    fastjson定制工程,别名配置化

    在“jxxt-fastjson”这个压缩包中,可能包含了实现这个功能的源代码、配置文件示例、以及相关的测试用例。通过分析这些内容,我们可以深入理解如何在实际项目中实施这个定制的Fastjson别名配置化方案。同时,了解这...

    Alibaba Fastjson Jar包.zip

    以下是一些使用Fastjson的基本示例: ```java // JSON字符串转Java对象 String jsonString = "{\"name\":\"John\", \"age\":30}"; Person person = JSON.parseObject(jsonString, Person.class); // Java对象转...

    fastjson.jar包资源

    - 示例代码:提供示例程序,演示如何使用Fastjson进行序列化和反序列化操作,以及其他的高级功能。 总结,Fastjson.jar作为一款高效且易用的Java JSON库,为开发者提供了强大的JSON处理能力。无论是简单的JSON数据...

    fastjson-1.2.35.jar

    总结,Fastjson-1.2.35.jar作为Fastjson的一个特定版本,不仅具备了Fastjson系列的所有优点,还对已知问题进行了修复和完善,为开发者提供了更稳定的JSON处理工具。在实际开发中,合理利用Fastjson的特性,可以显著...

    fastjson.pdf

    FastJSON提供了多种策略来处理序列化过程中的空值问题,可以根据实际需求选择最合适的处理方式。 #### 五、参考资料 1. **开源中国**: [FastJSON项目主页](http://www.oschina.net/p/fastjson) 2. **问题讨论**: ...

    fastJson开发包、源码及相关api文档

    ### 五、FastJson实战示例 ```java import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; public class FastJsonExample { public static void main(String[] args) { // 创建一个简单的...

    fastJSON

    博主通常会在这样的文章中分享关于如何使用fastJSON进行数据转换、优化性能以及解决常见问题的经验。由于没有实际内容可以引用,我们将基于fastJSON的一般知识来展开讨论。 1. **JSON简介**: JSON(JavaScript ...

    java FASTJSON 解析复杂JSON实例源码

    在这个实例中,我们将深入探讨如何使用Fastjson来解析复杂的JSON数据,并通过`JSONTest`这个示例代码来加深理解。 首先,让我们了解什么是JSON。JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,...

    fastJSON_v2.0.22(最新)

    **FastJSON_v2.0.22:一个高效且易用的JSON库** FastJSON是.NET平台上一个流行的、高性能的JSON解析器和生成器,它提供了简单易用的API,用于序列化和反序列化JSON对象。在版本v2.0.22中,这个库继续为开发者提供...

    fastjson-example:阿里巴巴fastjson示例。 一些演示以展示如何使用fastjson

    关于fastjson的使用示范案例 1.FastJson的介绍: FastJson是阿里巴巴的开源JSON解析库,它可以解析JSON格式的字符串,支持将Java Bean序列化为JSON字符串,也可以从JSON字符串反序列化到JavaBean。Github地址: 2....

    fastjson1.2.2.zip

    三、使用示例 1. **JSON字符串到Java对象** ```java String jsonString = "{\"name\":\"John\", \"age\":30}"; Person person = JSON.parseObject(jsonString, Person.class); ``` 2. **Java对象到JSON字符串** ...

    C#简单快速的json组件fastJSON使用介绍.docx

    在上面的示例中,我们首先创建了一个 zoo 对象,并添加了两个动物对象,然后使用 fastJSON 序列化了 zoo 对象,并将其反序列化回 zoo 对象。 fastJSON 序列化机制 fastJSON 序列化机制是基于反射和 IL 代码生成的...

    springboot2.0整合fastjson以及各种使用实例

    3. **使用示例**:在测试类中,我们可以编写各种使用Fastjson的方法。例如: - 序列化:将Java对象转换为JSON字符串。 ```java User user = new User(); user.setName("张三"); user.setAge(25); String json...

Global site tag (gtag.js) - Google Analytics