工程中所需的jar包,因?yàn)樵诰W(wǎng)上不太好找,所以我將它放到我的網(wǎng)盤(pán)里了,如有需要隨便下載。
1.簡(jiǎn)單的解析json字符串
首先將json字符串轉(zhuǎn)換為json對(duì)象,然后再解析json對(duì)象,過(guò)程如下。
1
|
JSONObject jsonObject = JSONObject.fromObject(jsonStr); |
根據(jù)json中的鍵得到它的值
1
2
3
4
|
String name = jsonObject.getString( "name" ); int num = jsonObject.getInt( "num" ); String sex = jsonObject.getString( "sex" ); int age = jsonObject.getInt( "age" ); |
2.將json字符串轉(zhuǎn)換為java對(duì)象
同樣先將json字符串轉(zhuǎn)換為json對(duì)象,再將json對(duì)象轉(zhuǎn)換為java對(duì)象,如下所示。
1
2
|
JSONObject obj = new JSONObject().fromObject(jsonStr); //將json字符串轉(zhuǎn)換為json對(duì)象 |
將json對(duì)象轉(zhuǎn)換為java對(duì)象
1
2
|
Person jb = (Person)JSONObject.toBean(obj,Person. class ); //將建json對(duì)象轉(zhuǎn)換為Person對(duì)象 |
3.將java對(duì)象轉(zhuǎn)換為json字符串
先將java對(duì)象轉(zhuǎn)換為json對(duì)象,在將json對(duì)象轉(zhuǎn)換為json字符串
1
2
3
4
|
JSONObject json = JSONObject.fromObject(obj); //將java對(duì)象轉(zhuǎn)換為json對(duì)象 String str = json.toString(); //將json對(duì)象轉(zhuǎn)換為字符串 |
完整代碼如下:
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
42
43
44
45
46
47
48
49
50
|
package baz.parse; import java.util.ArrayList; import java.util.List; import net.sf.json.JSON; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import net.sf.json.JSONSerializer; import baz.bean.Person; public class ParseJson { private String jsonStr; public ParseJson() { } public ParseJson(String str){ this .jsonStr = str; } /** * 解析json字符串 */ public void parse(){ JSONObject jsonObject = JSONObject.fromObject(jsonStr); String name = jsonObject.getString( "name" ); int num = jsonObject.getInt( "num" ); String sex = jsonObject.getString( "sex" ); int age = jsonObject.getInt( "age" ); System.out.println(name + " " + num + " " + sex + " " + age); } //將json字符串轉(zhuǎn)換為java對(duì)象 public Person JSON2Object(){ //接收{(diào)}對(duì)象,此處接收數(shù)組對(duì)象會(huì)有異常 if (jsonStr.indexOf( "[" ) != - 1 ){ jsonStr = jsonStr.replace( "[" , "" ); } if (jsonStr.indexOf( "]" ) != - 1 ){ jsonStr = jsonStr.replace( "]" , "" ); } JSONObject obj = new JSONObject().fromObject(jsonStr); //將json字符串轉(zhuǎn)換為json對(duì)象 Person jb = (Person)JSONObject.toBean(obj,Person. class ); //將建json對(duì)象轉(zhuǎn)換為Person對(duì)象 return jb; //返回一個(gè)Person對(duì)象 } } |
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
package baz.bean; public class Person { private String name; private int num; private String sex; private int age; public Person() { // TODO Auto-generated constructor stub } public Person(String name, int num, String sex, int age) { super (); this .name = name; this .num = num; this .sex = sex; this .age = age; } public String getName() { return name; } public void setName(String name) { this .name = name; } public int getNum() { return num; } public void setNum( int num) { this .num = num; } public String getSex() { return sex; } public void setSex(String sex) { this .sex = sex; } public int getAge() { return age; } public void setAge( int age) { this .age = age; } } |
將java對(duì)象轉(zhuǎn)換為json字符串
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
package baz.cons; import net.sf.json.JSONObject; /** * 將java對(duì)象轉(zhuǎn)換為json字符串 * @author Administrator * */ public class ConsJson { public ConsJson() { // TODO Auto-generated constructor stub } public String Object2Json(Object obj){ JSONObject json = JSONObject.fromObject(obj); //將java對(duì)象轉(zhuǎn)換為json對(duì)象 String str = json.toString(); //將json對(duì)象轉(zhuǎn)換為字符串 return str; } } |
測(cè)試類(lèi):
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
|
package baz.test; import java.util.List; import baz.bean.Person; import baz.cons.ConsJson; import baz.parse.ParseJson; public class Test { public static void main(String[] args) { //將字符串轉(zhuǎn)換為json對(duì)象,然后根據(jù)建得到相應(yīng)的值 ParseJson pj = new ParseJson( "{\"name\":\"gu\",\"num\":123456,\"sex\":\"male\",\"age\":24}" ); pj.parse(); //將一個(gè)json字符串轉(zhuǎn)換為java對(duì)象 Person p = pj.JSON2Object(); System.out.println( "Name:" + p.getName()); System.out.println( "Num:" + p.getNum()); System.out.println( "Sex:" + p.getSex()); System.out.println( "age:" + p.getAge()); //將一個(gè)java對(duì)象轉(zhuǎn)換為Json字符串 Person p1 = new Person( "gu1" , 123 , "male" , 23 ); ConsJson cj = new ConsJson(); String str1 = cj.Object2Json(p1); System.out.println(str1); } } |
測(cè)試輸出如下:
gu 123456 male 24
Name:gu
Num:123456
Sex:male
age:24
1
|
{ "age" :23, "name" : "gu1" , "num" :123, "sex" : "male" } |
這只是最簡(jiǎn)單使用方法,其他的使用我會(huì)在后期中更新。以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://blog.csdn.net/gchb9527/article/details/8688279