어제 우리가 피어 시스템과 인터페이스되었을 때, 피어 시스템은 우리가 통과 한 JSON 문자열을 처리 할 수 없었습니다. 나중에 그 이유는 우리가 통과 한 JSON 문자열에 JSON 객체 배열이 있어야했기 때문입니다. JSON 문자열이 테이블에 있기 때문에 JSON 문자열로 가져 와서 JSON 어레이에 넣으므로 이중 인용문으로 넣습니다. 피어는 그것이 JSON 객체가 아닌 문자열이라고 생각하므로 처리 할 수 없습니다.
[{ "cardname": "bankcard1", "cardcode": "888888888", "cardvalue": 999999999}, { "cardname": "bankcard2", "cardcode": "999999999999999999999999999", "Cardvalue"] this the the the the the the the the the the the the the the the th the the the the 개체, [ "{"cardname ":"bankcard1 ","cardcode ":"888888888 ","cardvalue ": 99999999}", "{"cardname ":"bankcard2 ","cardcode ":"999999999 ","cardvalue ","are a. " JSON 문자열 배열.다음은 Java를 JSON 스트링으로 변환하는 몇 가지 방법을 요약합니다.
1. Java 객체를 JSON 문자열로 변환합니다
2. jsonobject를 통해 JSON 문자열을 생성합니다
3. JSON 현을 통해 JSON 현을 생성합니다
이 코드는 Alibaba의 Fastjson 패키지를 통해 구현됩니다.
코드는 다음과 같습니다.
1. 사람 클래스, 크래드를 포함하는 목록
package com.doit8.test.jsontest.pojo; java.util.arraylist 가져 오기; Java.util.list 가져 오기; 공개 클래스 담당자 {개인 문자열 사용자 이름; 개인 문자열 이메일; 개인 문자열 섹스; 사적인 int 연령; 개인 목록 <cred> cardList = new ArrayList <crem> (); public String getUserName () {return username; } public void setusername (String username) {this.username = username; } public String getEmail () {반환 이메일; } public void seteMail (문자열 이메일) {this.email = 이메일; } public String getSex () {return sex; } public void setsex (String sex) {this.sex = sex; } public int getage () {반환 연령; } public void 설정 (int Age) {this.age = age; } public list <crect> getCardList () {return CardList; } public void setcardList (list <crect> cardList) {this.cardList = cardList; }}2. 카드 수업
package com.doit8.test.jsontest.pojo; 공개 클래스 카드 {private String cardname; 개인 문자열 카드 코드; 개인 정수 카드 볼루; 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 (정수 카드 볼로) {this.cardValue = cardValue; }}3. JSON 처리 테스트 클래스
패키지 com.doit8.test.jsontest; java.util.arraylist 가져 오기; Java.util.list 가져 오기; com.alibaba.fastjson.json import; com.alibaba.fastjson.jsonarray import; com.alibaba.fastjson.jsonobject import; import com.doit8.test.jsontest.pojo.card; com.doit8.test.jsontest.pojo.person import; /*** JSON 변환**/public class app {public static void main (String [] args) {// 1. 객체를 통해 JSON 문자열을 생성하고 객체에는 JSON 문자열로 변환 된 객체 배열이 포함되어 있습니다. 사람 = 새로운 사람 (); person.setusername ( "xiejava"); person.setsex ( "man"); person.setage (38); person.seteMail ( "[email protected]"); card1 = new Card (); card1.setcardname ( "bankcard1"); card1.setCardCode ( "888888888"); card1.SetCardValue (999999999); card2 = new Card (); card2.setCardName ( "bankcard1"); card2.setCardCode ( "9999999999"); card2.setCardValue (22222222222); // 객체 배열 목록 <Card> cards = new ArrayList <crect> (); cards.add (card1); cards.add (card2); person.setcardlist (카드); 문자열 json = json.tojson (person) .toString (); System.out.println (JSON); // 2. JSON 객체를 통해 JSON 문자열을 생성 jsonObject jobject = new JsonObject (); jobject.put ( "username", "xiejava"); jobject.put ( "섹스", "man"); jobject.put ( "Age", 38); jobject.put ( "이메일", "[email protected]"); // JSONARRAY jsonArray jarray를 통한 객체 배열 랩 랩 jarray = new JsonArray (); jarray.addall (카드); jobject.put ( "cardlist", jarray); 문자열 json2 = jobject.tojsonstring (); System.out.println (json2); // 3. json 객체 jsonobject jobject2 = new jsonobject ()를 통해 JSON 문자열을 생성합니다. jobject2.put ( "username", "xiejava"); jobject2.put ( "섹스", "man"); jobject2.put ( "Age", 38); jobject2.put ( "이메일", "[email protected]"); // json 문자열 cardjsonst1 = "{/"cardname/":/"bankcard1/",/"cardcode/":/"88888888/",/"cardvalue/": 99999999}"; String Cardjsonstr2 = "{/"cardname/":/"bankcard2/",/"cardcode/":/"999999999/",/"cardvalue/": 2222222222}"; json.parseobject (cardjsonst1); JSONARRAY JARRAY2 = NEW JSONARRAY (); // JSON 문자열을 JSON 객체로 변환하여 JSONARRAY에 추가합니다. [JSON.PARSEOBJECT () 메소드를 사용하여 JSON 객체로 변환해야합니다. 그렇지 않으면 문자열이되며 JSON 문자열로 변환 할 때 이중 인용이됩니다. ] jarray2.add (json.parseobject (cardjsonst1)); jarray2.add (json.parseobject (cardjsonstr2)); jobject2.put ( "cardlist", jarray2); 문자열 json3 = jobject2.tojsonstring (); System.out.println (json3); }}pom.xml에 Fastjson 패키지 소개
<pectionency> <groupid> com.alibaba </groupid> <artifactid> fastjson </artifactid> <bersion> 1.2.15 </version> </fectionency>
실행 결과
{ "cardlist": [{ "cardname": "bankcard1", "cardcode": "888888888", "cardvalue": 99999999}, { "cardname": "bankcard1", "cardcode ":"9999999999 ","cardvalue ": 2222222222222222222}],"섹스 ":"man ","man ","age ": 38,"email ":"[email protected] ","username ":"xiejava "}
{ "cardlist": [{ "cardcode": "8888888888", "cardname": "bankcard1", "cardvalue": 99999999}, { "cardcode": "999999999", "cardna ME ":"BankCard1 ","Cardvalue ": 22222222222}],"섹스 ":"man ","age ": 38,"이메일 ":"[email protected] ","사용자 이름 ":"xiejava "}
{ "cardlist": [{ "cardname": "bankcard1", "cardcode": "888888888", "cardvalue": 99999999}, { "cardname": "bankcard2", "cardcode ":"999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999UEN ": 222222222222222222222222222222222222221]