JSON 转换

相关jar包:http://download.csdn.net/detail/z345434645/8370369

新建Java bean

Response:
Java code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
public  class  Response {
     private  String error_code;
     private  String reason;
     private  Result result;
     /**
      * @return the error_code
      */
     public  String getError_code() {
         return  error_code;
     }
     /**
      * @param error_code the error_code to set
      */
     public  void  setError_code(String error_code) {
         this .error_code = error_code;
     }
     /**
      * @return the reason
      */
     public  String getReason() {
         return  reason;
     }
     /**
      * @param reason the reason to set
      */
     public  void  setReason(String reason) {
         this .reason = reason;
     }
     /**
      * @return the result
      */
     public  Result getResult() {
         return  result;
     }
     /**
      * @param result the result to set
      */
     public  void  setResult(Result result) {
         this .result = result;
     }
    

result:
Java code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public  class  Result {
     private  List<Data>data;
 
     /**
      * @return the data
      */
     public  List<Data> getData() {
         return  data;
     }
 
     /**
      * @param data the data to set
      */
     public  void  setData(List<Data> data) {
         this .data = data;
     }

data:

public class Data {
private String content;
private String hashid;
private Date unixtime;
private Date updatetime;
/**
 * @return the content
 */
public String getContent() {
return content;
}
/**
 * @param content the content to set
 */
public void setContent(String content) {
this.content = content;
}
/**
 * @return the hashid
 */
public String getHashid() {
return hashid;
}
/**
 * @param hashid the hashid to set
 */
public void setHashid(String hashid) {
this.hashid = hashid;
}
/**
 * @return the unixtime
 */
public Date getUnixtime() {
return unixtime;
}
/**
 * @param unixtime the unixtime to set
 */
public void setUnixtime(Date unixtime) {
this.unixtime = unixtime;
}
/**
 * @return the updatetime
 */
public Date getUpdatetime() {
return updatetime;
}
/**
 * @param updatetime the updatetime to set
 */
public void setUpdatetime(Date updatetime) {
this.updatetime = updatetime;
}



生成json字符串,如下:
Java code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Response response =  new  Response();
         response.setError_code( "0" );
         response.setReason( "Success" );
         Result result =  new  Result();
         Data data =  new  Data();
         data.setContent( "LZ的小儿子一岁多,经常在客厅的沙发垫子上拉粑粑,某日又拉在"
                 "了沙发上,刚好我把儿子洗好穿裤裤,我朋友就来了(门没上锁)一屁股就坐下去,我急忙说,那有屎"
                 "那有屎,他说,我也有事我也有事。。。。。。后来后来,我就说你真的一屁股有事了。。。。。" );
         data.setHashid( "2368E63987770452EA11F8A32BD06D96" );
         data.setUnixtime( new  Date());
         data.setUpdatetime( new  Date());
         List<Data>datas =  new  ArrayList<Data>();
         datas.add(data);
         result.setData(datas);
         response.setResult(result);
         
         JSONObject jsonObject = JSONObject.fromObject(response);
         System.out.println(jsonObject.toString());

打印结果为:
{"result":{
"data":[{"content":"LZ的小儿子一岁多,经常在客厅的沙发垫子上拉粑粑,某日又拉在了沙发上,刚好我把儿子洗好穿裤裤,我朋友就来了(门没上锁)一屁股就坐下去,我急忙说,那有屎那有屎,他说,我也有事我也有事。。。。。。后来后来,我就说你真的一屁股有事了。。。。。",
"unixtime":{"time":1421462054688,"minutes":34,"seconds":14,"hours":10,"month":0,"year":115,"timezoneOffset":-480,"day":6,"date":17},
"updatetime":{"time":1421462054688,"minutes":34,"seconds":14,"hours":10,"month":0,"year":115,"timezoneOffset":-480,"day":6,"date":17},
"hashid":"2368E63987770452EA11F8A32BD06D96"}]},
"reason":"Success",
"error_code":"0"}

我再把他转回JAVA bean,如下:
Java code
?
1
2
3
4
5
6
Response response2 =  new  Response();
         Map map =  new  HashedMap();
         map.put( "data" , Data. class );
         response2 = (Response) JSONObject.toBean(jsonObject, Response. class ,map);
         
         System.out.println(response2.getResult().getData());

可得如下结果:

然后可以进行下一步操作了。有疑问的欢迎留言
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值