相关jar包:http://download.csdn.net/detail/z345434645/8370369
新建Java bean
Response:
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:
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字符串,如下:
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,如下:
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());
|
可得如下结果:
![](http://img.bbs.csdn.net/upload/201501/17/1421462721_749574.jpg)
然后可以进行下一步操作了。有疑问的欢迎留言 。