Yesterday, when we were interfaced with the peer system, the peer system could not handle the json string we passed. Later, the reason was that there should be an array of json objects in the json string we passed. Because our json string is in the table, we took it out as a json string and put it in the json array, so we put it in double quotes. The peer thinks it is a string, not a json object, so it cannot be processed.
[{"cardName":"bankCard1","cardCode":"888888888","cardValue":999999999},{"cardName":"bankCard2","cardCode":"9999999999","cardValue":222222222222}] This is the correct array of json objects, ["{"cardName":"bankCard1","cardCode":"888888888","cardValue":999999999}","{"cardName":"bankCard2","cardCode":"9999999999","cardValue":2222222222}"], this is a json string array.The following summarizes several ways to convert Java to JSON strings:
1. Convert java object into json string
2. Generate json string through JSONObject
3. Generate json strings through json strings
The code is implemented through Alibaba's fastjson package.
The code is as follows:
1. Person class, List containing Crad
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 class
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 processing test class
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 conversion* */ public class App { public static void main( String[] args ) { //1. Generate a JSON string through an object, and the object contains an array of objects converted into a JSON string. Person person=new Person(); person.setUsername("xiejava"); person.setSex("man"); person.setAge(38); person.setEmail("[email protected]"); Card card1=new Card(); card1.setCardName("bankCard1"); card1.setCardCode("888888888"); card1.setCardValue(9999999999); Card card2=new Card(); card2.setCardName("bankCard1"); card2.setCardCode("9999999999"); card2.setCardValue(22222222222); //Object array 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. Generate JSON string through JSON objects JSONObject jObject=new JSONObject(); jObject.put("username", "xiejava"); jObject.put("sex", "man"); jObject.put("age", 38); jObject.put("email", "[email protected]"); //Wrap object array through JSONArray JSONArray jArray=new JSONArray(); jArray.addAll(cards); jObject.put("cardList", jArray); String json2=jObject.toJSONString(); System.out.println(json2); //3. Generate JSON string through JSON object JSONObject jObject2=new JSONObject(); jObject2.put("username", "xiejava"); jObject2.put("sex", "man"); jObject2.put("age", 38); jObject2.put("email", "[email protected]"); //Construct JSON string String cardjsonStr1="{/"cardName/":/"bankCard1/",/"cardCode/":/"88888888/",/"cardValue/":999999999}"; String cardjsonStr2="{/"cardName/":/"bankCard2/",/"cardCode/":/"999999999/",/"cardValue/":2222222222}"; JSON.parseObject(cardjsonStr1); JSONArray jArray2=new JSONArray(); //Convert the JSON string to a JSON object and add it to JSONArray. [Note that you must use the JSON.parseObject() method to convert it to a JSON object, otherwise it will still be a string, and it will be a double quote when converting it to a JSON string. ] jArray2.add(JSON.parseObject(cardjsonStr1)); jArray2.add(JSON.parseObject(cardjsonStr2)); jObject2.put("cardList", jArray2); String json3=jObject2.toJSONString(); System.out.println(json3); } }Introducing fastjson package in pom.xml
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.15</version> </dependency>
Running results
{"cardList":[{"cardName":"bankCard1","cardCode":"888888888","cardValue":999999999},{"cardName":"bankCard1","cardCode":"9999999999","cardValue":222222222222}],"sex":"man","age":38,"email":"[email protected]","username":"xiejava"}
{"cardList":[{"cardCode":"8888888888","cardName":"bankCard1","cardValue":999999999},{"cardCode":"9999999999","cardName":"bankCard1","cardValue":22222222222}],"sex":"man","age":38,"email":"[email protected]","username":"xiejava"}
{"cardList":[{"cardName":"bankCard1","cardCode":"888888888","cardValue":999999999},{"cardName":"bankCard2","cardCode":"99999999999","cardValue":22222222222}],"sex":"man","age":38,"email":"[email protected]","username":"xiejava"}