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

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