JSON字符串与对象的转化问题
2016-05-18 11:29
435 查看
1、普通的JSON字符串与对象的转化
2、包含list对象的JSON字符串 与对象的转化
3、包含对象的JSON字符串与对象的转化
所用到的实体类
package com.demo.json.object;
public class JSONObject {
private String code;
private String reqdate;
private String rescode;
private String resmsg;
private String seqno;
private String version;
}
========================================================================================
package com.demo.json.object;
public class JSONObject2 {
private String name;
private String password;
private String lists;
}
此处省略get、set 方法
1、
import com.alibaba.fastjson.JSON;
public static void main(String[] args) {
String code = "KICKOFF";
String reqdate = "2016-04-28 16:37:26";
String rescode = "-3";
String resmsg = "auth error";
String seqno = "1234567";
String version = "bd1.0";
JSONObject json1 = new JSONObject();
json1.setCode(code);
json1.setReqdate(reqdate);
json1.setRescode(rescode);
json1.setResmsg(resmsg);
json1.setSeqno(seqno);
json1.setVersion(version);
// 对象转化成字符串
String reqJson = JSON.toJSONString(json1);
System.out.println(reqJson);
// 字符串转化为对象
JSONObject jsonobject = JSON.parseObject(reqJson, JSONObject.class);
System.out.println(jsonobject.toString());
//
{"code":"KICKOFF","reqdate":"2016-04-28 16:37:26","rescode":"-3","resmsg":"auth error","seqno":"1234567","version":"bd1.0"}
// JSONObject [code=KICKOFF, reqdate=2016-04-28 16:37:26, rescode=-3,
// resmsg=auth error, seqno=1234567, version=bd1.0]
}
2、
public static void main(String[] args) {
String code = "KICKOFF";
String reqdate = "2016-04-28 16:37:26";
String rescode = "-3";
String resmsg = "auth error";
String seqno = "1234567";
String version = "bd1.0";
JSONObject json1 = new JSONObject();
json1.setCode(code);
json1.setReqdate(reqdate);
json1.setRescode(rescode);
json1.setResmsg(resmsg);
json1.setSeqno(seqno);
json1.setVersion(version);
JSONObject json2 = new JSONObject();
json2.setCode(code);
json2.setReqdate(reqdate);
json2.setRescode(rescode);
json2.setResmsg(resmsg);
json2.setSeqno(seqno);
json2.setVersion(version);
List<JSONObject> list = new ArrayList<JSONObject>();
list.add(json1);
list.add(json2);
String list1 = JSONArray.fromObject(list).toString();
JSONObject2 jsonObject2 = new JSONObject2();
jsonObject2.setLists(list1);
jsonObject2.setName("yexiaojing");
jsonObject2.setPassword("yexiaojingmima");
// 对象转化成字符串
String reqJson = JSON.toJSONString(jsonObject2);
System.out.println(reqJson);
// 字符串转化为对象
JSONObject2 jsonobject2 = JSON.parseObject(reqJson, JSONObject2.class);
System.out.println(jsonobject2.toString());
String list2 = jsonobject2.getLists();
System.out.println(list2);
List<JSONObject> list3 = JSON.parseArray(list2, JSONObject.class);
for (JSONObject jsonObject : list3) {
System.out.println(jsonObject.toString());
}
3、
public static void main(String[] args) {
String code = "KICKOFF";
String reqdate = "2016-04-28 16:37:26";
String rescode = "-3";
String resmsg = "auth error";
String seqno = "1234567";
String version = "bd1.0";
JSONObject json1 = new JSONObject();
json1.setCode(code);
json1.setReqdate(reqdate);
json1.setRescode(rescode);
json1.setResmsg(resmsg);
json1.setSeqno(seqno);
json1.setVersion(version);
JSONObject2 jsonObject2 = new JSONObject2();
jsonObject2.setLists(JSON.toJSONString(json1));
jsonObject2.setName("yexiaojing");
jsonObjec
4000
t2.setPassword("yexiaojingmima");
// 对象转化成字符串
String reqJson = JSON.toJSONString(jsonObject2);
System.out.println(reqJson);
// 字符串转化为对象
JSONObject2 jsonobject2 = JSON.parseObject(reqJson, JSONObject2.class);
System.out.println(jsonobject2.toString());
String list2 = jsonobject2.getLists();
System.out.println(list2);
JSONObject jsonObject = JSON.parseObject(list2, JSONObject.class);
System.out.println(jsonObject.toString());
// {"lists":"{\"code\":\"KICKOFF\",\"reqdate\":\"2016-04-28 16:37:26\",\"rescode\":\"-3\",\"resmsg\":\"auth error\",\"seqno\":\"1234567\",\"version\":\"bd1.0\"}","name":"yexiaojing","password":"yexiaojingmima"}
// com.demo.json.object.JSONObject2@55d93d
// {"code":"KICKOFF","reqdate":"2016-04-28 16:37:26","rescode":"-3","resmsg":"auth error","seqno":"1234567","version":"bd1.0"}
// JSONObject [code=KICKOFF, reqdate=2016-04-28 16:37:26, rescode=-3,
// resmsg=auth error, seqno=1234567, version=bd1.0]
}
2、包含list对象的JSON字符串 与对象的转化
3、包含对象的JSON字符串与对象的转化
所用到的实体类
package com.demo.json.object;
public class JSONObject {
private String code;
private String reqdate;
private String rescode;
private String resmsg;
private String seqno;
private String version;
}
========================================================================================
package com.demo.json.object;
public class JSONObject2 {
private String name;
private String password;
private String lists;
}
此处省略get、set 方法
1、
import com.alibaba.fastjson.JSON;
public static void main(String[] args) {
String code = "KICKOFF";
String reqdate = "2016-04-28 16:37:26";
String rescode = "-3";
String resmsg = "auth error";
String seqno = "1234567";
String version = "bd1.0";
JSONObject json1 = new JSONObject();
json1.setCode(code);
json1.setReqdate(reqdate);
json1.setRescode(rescode);
json1.setResmsg(resmsg);
json1.setSeqno(seqno);
json1.setVersion(version);
// 对象转化成字符串
String reqJson = JSON.toJSONString(json1);
System.out.println(reqJson);
// 字符串转化为对象
JSONObject jsonobject = JSON.parseObject(reqJson, JSONObject.class);
System.out.println(jsonobject.toString());
//
{"code":"KICKOFF","reqdate":"2016-04-28 16:37:26","rescode":"-3","resmsg":"auth error","seqno":"1234567","version":"bd1.0"}
// JSONObject [code=KICKOFF, reqdate=2016-04-28 16:37:26, rescode=-3,
// resmsg=auth error, seqno=1234567, version=bd1.0]
}
2、
public static void main(String[] args) {
String code = "KICKOFF";
String reqdate = "2016-04-28 16:37:26";
String rescode = "-3";
String resmsg = "auth error";
String seqno = "1234567";
String version = "bd1.0";
JSONObject json1 = new JSONObject();
json1.setCode(code);
json1.setReqdate(reqdate);
json1.setRescode(rescode);
json1.setResmsg(resmsg);
json1.setSeqno(seqno);
json1.setVersion(version);
JSONObject json2 = new JSONObject();
json2.setCode(code);
json2.setReqdate(reqdate);
json2.setRescode(rescode);
json2.setResmsg(resmsg);
json2.setSeqno(seqno);
json2.setVersion(version);
List<JSONObject> list = new ArrayList<JSONObject>();
list.add(json1);
list.add(json2);
String list1 = JSONArray.fromObject(list).toString();
JSONObject2 jsonObject2 = new JSONObject2();
jsonObject2.setLists(list1);
jsonObject2.setName("yexiaojing");
jsonObject2.setPassword("yexiaojingmima");
// 对象转化成字符串
String reqJson = JSON.toJSONString(jsonObject2);
System.out.println(reqJson);
// 字符串转化为对象
JSONObject2 jsonobject2 = JSON.parseObject(reqJson, JSONObject2.class);
System.out.println(jsonobject2.toString());
String list2 = jsonobject2.getLists();
System.out.println(list2);
List<JSONObject> list3 = JSON.parseArray(list2, JSONObject.class);
for (JSONObject jsonObject : list3) {
System.out.println(jsonObject.toString());
}
3、
public static void main(String[] args) {
String code = "KICKOFF";
String reqdate = "2016-04-28 16:37:26";
String rescode = "-3";
String resmsg = "auth error";
String seqno = "1234567";
String version = "bd1.0";
JSONObject json1 = new JSONObject();
json1.setCode(code);
json1.setReqdate(reqdate);
json1.setRescode(rescode);
json1.setResmsg(resmsg);
json1.setSeqno(seqno);
json1.setVersion(version);
JSONObject2 jsonObject2 = new JSONObject2();
jsonObject2.setLists(JSON.toJSONString(json1));
jsonObject2.setName("yexiaojing");
jsonObjec
4000
t2.setPassword("yexiaojingmima");
// 对象转化成字符串
String reqJson = JSON.toJSONString(jsonObject2);
System.out.println(reqJson);
// 字符串转化为对象
JSONObject2 jsonobject2 = JSON.parseObject(reqJson, JSONObject2.class);
System.out.println(jsonobject2.toString());
String list2 = jsonobject2.getLists();
System.out.println(list2);
JSONObject jsonObject = JSON.parseObject(list2, JSONObject.class);
System.out.println(jsonObject.toString());
// {"lists":"{\"code\":\"KICKOFF\",\"reqdate\":\"2016-04-28 16:37:26\",\"rescode\":\"-3\",\"resmsg\":\"auth error\",\"seqno\":\"1234567\",\"version\":\"bd1.0\"}","name":"yexiaojing","password":"yexiaojingmima"}
// com.demo.json.object.JSONObject2@55d93d
// {"code":"KICKOFF","reqdate":"2016-04-28 16:37:26","rescode":"-3","resmsg":"auth error","seqno":"1234567","version":"bd1.0"}
// JSONObject [code=KICKOFF, reqdate=2016-04-28 16:37:26, rescode=-3,
// resmsg=auth error, seqno=1234567, version=bd1.0]
}
相关文章推荐
- JavaScript小技巧
- JS中eval()函数处理返回的json数据
- JavaScript 基础(五) 函数 变量和作用域
- 用JS实现倒计时(日期字符串作为参数)
- 浏览器兼容处理(HTML条件注释、CSSHack和JS识别)
- 12个非常实用的JavaScript小技巧【推荐】
- js正则表达式验证大全--转载
- JavaScript设计模式开发中组合模式的使用教程
- ionic中文javascript API(2)
- JavaScript归并方法reduce()和reduceRight()
- 设计模式中的组合模式在JavaScript程序构建中的使用
- cJSON学习笔记(一)
- 12个非常实用的JavaScript小技巧
- jsp
- extjs 插入echarts柱状图(附带一个饼图)
- 《Secrets of the JavaScript Ninja》:JavaScript 之运行时代码
- CJSON学习笔记(二)
- javascript学习——闭包
- js数据类型,及内存存储学习笔记
- javascript对象属性的赋值解析