- 浏览: 1564558 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (673)
- Linux (3)
- android (302)
- php (12)
- struts2 (1)
- Flex积累 (88)
- Hibernate (8)
- MyEclipse (18)
- android备忘录 (209)
- Java备忘录 (53)
- spring (0)
- Flex备忘录 (30)
- JavaScript (10)
- Tomcat (11)
- Flex FrameWork (0)
- 设计模式学习 (4)
- 休闲娱乐 (4)
- Flex skin (1)
- Flex 组件 (2)
- 持久化相关 (2)
- DotNet相关 (5)
- Flex Style (1)
- Windows (11)
- Flex AIR (9)
- MySQL (1)
- mac OSX (29)
- eclipse (26)
- database (5)
- ARM (1)
- English (1)
- WebService (8)
- javabase64 (2)
- ruby (1)
- sqlite (5)
- ios (7)
- 无线应用 (1)
- Java (3)
- goagent (1)
- oc (1)
最新评论
-
Z_ying:
谢谢分享,上次也遇到过的,解决了,过了一段时间又忘记,幸亏看到 ...
FastJson 示例 及问题 -
wto8559:
这是什么错啊,我也遇到了.问下写一个servlet能解决什么问 ...
Exception in thread "HouseKeeper" java.lang.NullPointerException -
zxjlwt:
如何用php怎么弄了。http://surenpi.com
\U672a\U77e5\U9519\U8bef 字符还原 -
windseamless:
那怎么样才能屏蔽呢
java.lang.IllegalArgumentException: Window type can not be changed after the -
macleo:
Trinea 写道还是利用Fiddler抓包更方便,(1)无需 ...
android网络抓包相关
不能以内部类的形式,使用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
========================================================
===========================================================================
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
========================================================
发表评论
-
再次android 下的Caused by: java.lang.ClassNotFoundException:
2013-06-05 11:34 2366莫名其妙发生了:Caused by: java.lang.Cl ... -
android RatingBar 收集
2013-05-29 09:50 987RatingBar例子: http://www.eoeandr ... -
java 发送邮件
2013-05-28 16:17 878http://www.blogjava.net/wangfun ... -
ExecutorService 建立多线程的步骤
2013-05-26 22:18 882http://huangxiaojian9999.blog.1 ... -
android include报错解决一例
2013-05-24 13:48 1196一个layout 嵌入另外一个layout < ... -
.net 接口报错解决一例
2013-05-24 13:44 954NullReferenceException: 未将对象引用设 ... -
Overlay移动收集
2013-05-19 17:04 9091.http://changyy.pixnet.net/blo ... -
模拟器 INSTALL_FAILED_CONTAINER_ERROR
2013-05-10 00:21 1573用模拟器跑,总是报这个错。 找了一下,发现是androidma ... -
百度地图总结
2013-05-02 11:34 10711.mMapView.regMapViewListener(a ... -
EditText SetError 4.0下 不显示文字解决
2013-04-24 16:46 2045http://www.eoeandroid.com/forum ... -
短信验证
2013-04-23 15:27 941解决方案如下: http://www.eoeandroid.c ... -
根据两个经纬度计算两地距离
2013-04-23 14:56 1673com.baidu.mapapi.utils.Distance ... -
百度地图 android 线路
2013-04-22 23:39 2490最好的资料莫过于它自己提供的了 -2.管理自己的key htt ... -
android 屏幕方向总结
2013-04-17 15:23 989http://dev.wo.com.cn/docportal/ ... -
分享总结
2013-04-15 17:28 8471。http://www.umeng.com/componen ... -
sqlitemanager
2013-03-31 23:56 1073http://blog.csdn.net/jjhua/arti ... -
android 字体样式
2013-03-20 13:17 1121http://tank03.iteye.com/blog/13 ... -
Android 之 下拉框(Spinner)的使用
2013-03-19 21:39 1061http://imshare.iteye.com/blog/7 ... -
Android 为【apk】文件签名,增加修改系统时间等权限
2013-03-18 18:09 1134Android 为【apk】文件签名,增加修改系统时间等权限 ... -
Mac OS X 10.6 下 android 源码编译
2013-03-18 15:36 1020http://blog.csdn.net/edai9508/a ...
相关推荐
**FastJson:一个高效、强大的Java JSON库** FastJson是阿里巴巴开源的一款高性能的JSON库,它主要用于Java对象与JSON字符串之间的转换。在这个小例子中,我们将深入探讨FastJson的使用方法,以及它如何帮助开发者...
在Android开发中,数据交换和序列化是常见的需求,FastJSON是阿里巴巴提供的一款高效...通过以上示例,你应该对如何在Android Studio中使用FastJSON有了初步了解。在实践中不断探索,你会发现更多FastJSON的便捷之处。
Fastjson是阿里巴巴开发的一款高效、功能强大的JSON库,它在Java世界中被广泛应用,尤其在大规模服务器...在实际项目中,结合提供的API和示例代码(如`FastJsonDemo`),开发者可以快速上手并熟练掌握Fastjson的使用。
在这个示例中,我们将深入探讨Fastjson的核心功能、使用方法以及在实际项目中的应用。 首先,Fastjson 的主要功能包括 JSON 字符串与 Java 对象之间的相互转换。通过 `parseObject()` 方法,可以将 JSON 字符串解析...
这个示例源码对于学习和实践SpringMVC、Fastjson和Swagger的集成非常有帮助,它展示了如何在实际项目中有效地利用这些工具,提升开发效率和API的质量。开发者可以通过阅读和运行代码,更深入地理解这三个组件的协同...
《深入解析Fastjson:1.2.47版本与源码分析及简单示例》 在Java开发中,处理JSON数据是一项常见的任务。Fastjson,作为阿里巴巴开源的一个高性能的JSON库,因其简单易用和高效的特性,深受广大开发者喜爱。本文将...
三、Fastjson使用示例 1. **JSON字符串转Java对象** ```java String jsonString = "{\"name\":\"张三\",\"age\":25}"; Person person = JSON.parseObject(jsonString, Person.class); ``` 2. **Java对象转JSON...
在“jxxt-fastjson”这个压缩包中,可能包含了实现这个功能的源代码、配置文件示例、以及相关的测试用例。通过分析这些内容,我们可以深入理解如何在实际项目中实施这个定制的Fastjson别名配置化方案。同时,了解这...
以下是一些使用Fastjson的基本示例: ```java // JSON字符串转Java对象 String jsonString = "{\"name\":\"John\", \"age\":30}"; Person person = JSON.parseObject(jsonString, Person.class); // Java对象转...
- 示例代码:提供示例程序,演示如何使用Fastjson进行序列化和反序列化操作,以及其他的高级功能。 总结,Fastjson.jar作为一款高效且易用的Java JSON库,为开发者提供了强大的JSON处理能力。无论是简单的JSON数据...
总结,Fastjson-1.2.35.jar作为Fastjson的一个特定版本,不仅具备了Fastjson系列的所有优点,还对已知问题进行了修复和完善,为开发者提供了更稳定的JSON处理工具。在实际开发中,合理利用Fastjson的特性,可以显著...
FastJSON提供了多种策略来处理序列化过程中的空值问题,可以根据实际需求选择最合适的处理方式。 #### 五、参考资料 1. **开源中国**: [FastJSON项目主页](http://www.oschina.net/p/fastjson) 2. **问题讨论**: ...
### 五、FastJson实战示例 ```java import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; public class FastJsonExample { public static void main(String[] args) { // 创建一个简单的...
博主通常会在这样的文章中分享关于如何使用fastJSON进行数据转换、优化性能以及解决常见问题的经验。由于没有实际内容可以引用,我们将基于fastJSON的一般知识来展开讨论。 1. **JSON简介**: JSON(JavaScript ...
在这个实例中,我们将深入探讨如何使用Fastjson来解析复杂的JSON数据,并通过`JSONTest`这个示例代码来加深理解。 首先,让我们了解什么是JSON。JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,...
**FastJSON_v2.0.22:一个高效且易用的JSON库** FastJSON是.NET平台上一个流行的、高性能的JSON解析器和生成器,它提供了简单易用的API,用于序列化和反序列化JSON对象。在版本v2.0.22中,这个库继续为开发者提供...
关于fastjson的使用示范案例 1.FastJson的介绍: FastJson是阿里巴巴的开源JSON解析库,它可以解析JSON格式的字符串,支持将Java Bean序列化为JSON字符串,也可以从JSON字符串反序列化到JavaBean。Github地址: 2....
三、使用示例 1. **JSON字符串到Java对象** ```java String jsonString = "{\"name\":\"John\", \"age\":30}"; Person person = JSON.parseObject(jsonString, Person.class); ``` 2. **Java对象到JSON字符串** ...
在上面的示例中,我们首先创建了一个 zoo 对象,并添加了两个动物对象,然后使用 fastJSON 序列化了 zoo 对象,并将其反序列化回 zoo 对象。 fastJSON 序列化机制 fastJSON 序列化机制是基于反射和 IL 代码生成的...
3. **使用示例**:在测试类中,我们可以编写各种使用Fastjson的方法。例如: - 序列化:将Java对象转换为JSON字符串。 ```java User user = new User(); user.setName("张三"); user.setAge(25); String json...