昨天在與對端系統調接口的時候,對端系統對我們傳過去的json串老是處理不了,后來查原因是應為我們傳過去的json串里有json對象數組,因為我們的json串存在表里的,取出來是作為json字符串放到json數組里的,所以帶了雙引號,對端認為是字符串,不是json對象所以處理不了。
1
|
[{ "cardName" : "bankCard1" , "cardCode" : "888888888" , "cardValue" : 99999999 },{ "cardName" : "bankCard2" , "cardCode" : "999999999" , "cardValue" : 222222222 }] 這種是正確的json對象數組,[ "{" cardName ":" bankCard1 "," cardCode ":" 888888888 "," cardValue ":99999999}" , "{" cardName ":" bankCard2 "," cardCode ":" 999999999 "," cardValue ":222222222}" ],這種是json字符串數組。 |
以下總結一下java轉JSON串的幾種方式:
1、將java對象轉成json串
2、通過JSONObject生成json串
3、通過json字符串生成json串
代碼通過阿里的fastjson包實現。
代碼如下:
1、Person類,包含Crad的List
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
|
package com.doit8.test.jsontest.pojo; import java.util.ArrayList; import java.util.List; public class Person { private String username; private String email; private String sex; private int age; private List<Card> cardList= new ArrayList<Card>(); public String getUsername() { return username; } public void setUsername(String username) { this .username = username; } public String getEmail() { return email; } public void setEmail(String email) { this .email = email; } 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; } public List<Card> getCardList() { return cardList; } public void setCardList(List<Card> cardList) { this .cardList = cardList; } } |
2、Card類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
package com.doit8.test.jsontest.pojo; public class Card { private String cardName; private String cardCode; private Integer cardValue; public String getCardName() { return cardName; } public void setCardName(String cardName) { this .cardName = cardName; } public String getCardCode() { return cardCode; } public void setCardCode(String cardCode) { this .cardCode = cardCode; } public Integer getCardValue() { return cardValue; } public void setCardValue(Integer cardValue) { this .cardValue = cardValue; } } |
3、JSON處理測試類
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
57
58
59
60
61
62
63
64
65
66
67
68
|
package com.doit8.test.jsontest; import java.util.ArrayList; import java.util.List; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.doit8.test.jsontest.pojo.Card; import com.doit8.test.jsontest.pojo.Person; /** * JSON轉換 * */ public class App { public static void main( String[] args ) { //1。通過對象生成JSON串,對象里包含對象數組轉成JSON串。 Person person= new Person(); person.setUsername( "xiejava" ); person.setSex( "man" ); person.setAge( 38 ); Card card1= new Card(); card1.setCardName( "bankCard1" ); card1.setCardCode( "888888888" ); card1.setCardValue( 99999999 ); Card card2= new Card(); card2.setCardName( "bankCard1" ); card2.setCardCode( "999999999" ); card2.setCardValue( 222222222 ); //對象數組 List<Card> cards= new ArrayList<Card>(); cards.add(card1); cards.add(card2); person.setCardList(cards); String json = JSON.toJSON(person).toString(); System.out.println(json); //2.通過JSON對象生成JSON串 JSONObject jObject= new JSONObject(); jObject.put( "username" , "xiejava" ); jObject.put( "sex" , "man" ); jObject.put( "age" , 38 ); //通過JSONArray包裝對象數組 JSONArray jArray= new JSONArray(); jArray.addAll(cards); jObject.put( "cardList" , jArray); String json2=jObject.toJSONString(); System.out.println(json2); //3.通過JSON對象生成JSON串 JSONObject jObject2= new JSONObject(); jObject2.put( "username" , "xiejava" ); jObject2.put( "sex" , "man" ); jObject2.put( "age" , 38 ); //構造JSON字符串 String cardjsonStr1= "{\"cardName\":\"bankCard1\",\"cardCode\":\"888888888\",\"cardValue\":99999999}" ; String cardjsonStr2= "{\"cardName\":\"bankCard2\",\"cardCode\":\"999999999\",\"cardValue\":222222222}" ; JSON.parseObject(cardjsonStr1); JSONArray jArray2= new JSONArray(); //將JSON字符串轉成JSON對象,加入到JSONArray,[注意一定要用JSON.parseObject()方法轉換成JSON對象,否則還是字符串,轉成JSON串的時候會帶雙引號。] jArray2.add(JSON.parseObject(cardjsonStr1)); jArray2.add(JSON.parseObject(cardjsonStr2)); jObject2.put( "cardList" , jArray2); String json3=jObject2.toJSONString(); System.out.println(json3); } } |
pom.xml中引入fastjson包
1
2
3
4
5
|
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version> 1.2 . 15 </version> </dependency> |
運行結果
{"cardList":[{"cardName":"bankCard1","cardCode":"888888888","cardValue":99999999},{"cardName":"bankCard1","cardCode":"999999999","cardValue":222222222}],"sex":"man","age":38,"email":"[email protected]","username":"xiejava"}
{"cardList":[{"cardCode":"888888888","cardName":"bankCard1","cardValue":99999999},{"cardCode":"999999999","cardName":"bankCard1","cardValue":222222222}],"sex":"man","age":38,"email":"[email protected]","username":"xiejava"}
{"cardList":[{"cardName":"bankCard1","cardCode":"888888888","cardValue":99999999},{"cardName":"bankCard2","cardCode":"999999999","cardValue":222222222}],"sex":"man","age":38,"email":"[email protected]","username":"xiejava"}