2016-12-01 17 views
1

私はJson StringをObjectクラスに変換したいと思っています。私はいくつかのコードで数回試してみましたが、エラーが出ました。また、今そのリスト複雑なJson文字列をオブジェクトに変換する方法

インフォアのためヌルを与えるには、ここで

下回っている私のJSON

ここ
{ 
    "customer": { 
     "msisdn": "123456789", 
     "imsi": "0987654321", 
     "title": "Mr", 
     "firstName": "Bilbo", 
     "lastName": "Baggins", 
     "dob": "21/10/2006", 
     "identificationType": "PP", 
     "identificationNumber": "PP12345DC", 
     "onBehalfOf": "my_Merchant", 
     "purchaseCategoryCode": "Game", 
     "accountType": "Postpaid", 
     "ownerType": "Paymaster", 
     "status": "Confirmed", 
     "requestIdentifier": "REQ12345678", 
     "responseIdentifier": "RES12345678", 
     "address": { 
      "line1": "Bag End", 
      "line2": "", 
      "line3": "", 
      "city": "The Shire", 
      "country": "Middle Earth" 
     }, 
     "additionalInfo ": [{ 
      "tag": "creditLimit", 
      "value": "2500" 
     }, { 
      "tag": "creditLimit", 
      "value": "2500" 
     }], 
     "resourceURL": "http://google.com/abc/123" 
    } 
} 

は私のメインクラスコード

public class TestJsonDemo{ 
    public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException { 
     String testString="{\r\n" + 
       " \"customer\": {\r\n" + 
       "  \"msisdn\": \"123456789\",\r\n" + 
       "  \"imsi\": \"0987654321\",\r\n" + 
       "  \"title\": \"Mr\",\r\n" + 
       "  \"firstName\": \"Bilbo\",\r\n" + 
       "  \"lastName\": \"Baggins\",\r\n" + 
       "  \"dob\": \"21/10/2006\",\r\n" + 
       "  \"identificationType\": \"PP\",\r\n" + 
       "  \"identificationNumber\": \"PP12345DC\",\r\n" + 
       "  \"onBehalfOf\": \"my_Merchant\",\r\n" + 
       "  \"purchaseCategoryCode\": \"Game\",\r\n" + 
       "  \"accountType\": \"Postpaid\",\r\n" + 
       "  \"ownerType\": \"Paymaster\",\r\n" + 
       "  \"status\": \"Confirmed\",\r\n" + 
       "  \"requestIdentifier\": \"REQ12345678\",\r\n" + 
       "  \"responseIdentifier\": \"RES12345678\",\r\n" + 
       "  \"address\": {\r\n" + 
       "   \"line1\": \"Bag End\",\r\n" + 
       "   \"line2\": \"\",\r\n" + 
       "   \"line3\": \"\",\r\n" + 
       "   \"city\": \"The Shire\",\r\n" + 
       "   \"country\": \"Middle Earth\"\r\n" + 
       "  },\r\n" + 
       "  \"additionalInfo \": [{\r\n" + 
       "   \"tag\": \"creditLimit\",\r\n" + 
       "   \"value\": \"2500\"\r\n" + 
       "  }, {\r\n" + 
       "   \"tag\": \"creditLimit\",\r\n" + 
       "   \"value\": \"2500\"\r\n" + 
       "  }],\r\n" + 
       "  \"resourceURL\": \"http://google.com/abc/123\"\r\n" + 
       " }\r\n" + 
       "}"; 

     Gson gson=new Gson(); 
     CustomerInfo customerInfo = gson.fromJson(testString, CustomerInfo.class); 
     System.out.println(customerInfo); 
    } 
} 

はここにある私のうち

です
ClassPojo [customer = ClassPojo [lastName = Baggins, additionalInfo = null, resourceURL = http://google.com/abc/123, status = Confirmed, msisdn = 123456789, ownerType = Paymaster, identificationType = PP, onBehalfOf = my_Merchant, responseIdentifier = RES12345678, requestIdentifier = REQ12345678, title = Mr, address = ClassPojo [line1 = Bag End, line3 = , country = Middle Earth, city = The Shire, line2 = ], dob = 21/10/2006, purchaseCategoryCode = Game, accountType = Postpaid, imsi = 0987654321, firstName = Bilbo, identificationNumber = PP12345DC]] 

しかし、そのadditionalInfo = null :-(

これを解決するには?私も、残りのPOJOクラスを添付します

おかげ

class CustomerInfo { 
    private Customer customer; 

    public Customer getCustomer() { 
     return customer; 
    } 

    public void setCustomer(Customer customer) { 
     this.customer = customer; 
    } 

    @Override 
    public String toString() { 
     return "ClassPojo [customer = " + customer + "]"; 
    } 
} 

class Customer { 
    private String lastName; 

    private AdditionalInfo[] additionalInfo; 

    private String resourceURL; 

    private String status; 

    private String msisdn; 

    private String ownerType; 

    private String identificationType; 

    private String onBehalfOf; 

    private String responseIdentifier; 

    private String requestIdentifier; 

    private String title; 

    private Address address; 

    private String dob; 

    private String purchaseCategoryCode; 

    private String accountType; 

    private String imsi; 

    private String firstName; 

    private String identificationNumber; 

    public String getLastName() { 
     return lastName; 
    } 

    public void setLastName(String lastName) { 
     this.lastName = lastName; 
    } 

    public AdditionalInfo[] getAdditionalInfo() { 
     return additionalInfo; 
    } 

    public void setAdditionalInfo(AdditionalInfo[] additionalInfo) { 
     this.additionalInfo = additionalInfo; 
    } 

    public String getResourceURL() { 
     return resourceURL; 
    } 

    public void setResourceURL(String resourceURL) { 
     this.resourceURL = resourceURL; 
    } 

    public String getStatus() { 
     return status; 
    } 

    public void setStatus(String status) { 
     this.status = status; 
    } 

    public String getMsisdn() { 
     return msisdn; 
    } 

    public void setMsisdn(String msisdn) { 
     this.msisdn = msisdn; 
    } 

    public String getOwnerType() { 
     return ownerType; 
    } 

    public void setOwnerType(String ownerType) { 
     this.ownerType = ownerType; 
    } 

    public String getIdentificationType() { 
     return identificationType; 
    } 

    public void setIdentificationType(String identificationType) { 
     this.identificationType = identificationType; 
    } 

    public String getOnBehalfOf() { 
     return onBehalfOf; 
    } 

    public void setOnBehalfOf(String onBehalfOf) { 
     this.onBehalfOf = onBehalfOf; 
    } 

    public String getResponseIdentifier() { 
     return responseIdentifier; 
    } 

    public void setResponseIdentifier(String responseIdentifier) { 
     this.responseIdentifier = responseIdentifier; 
    } 

    public String getRequestIdentifier() { 
     return requestIdentifier; 
    } 

    public void setRequestIdentifier(String requestIdentifier) { 
     this.requestIdentifier = requestIdentifier; 
    } 

    public String getTitle() { 
     return title; 
    } 

    public void setTitle(String title) { 
     this.title = title; 
    } 

    public Address getAddress() { 
     return address; 
    } 

    public void setAddress(Address address) { 
     this.address = address; 
    } 

    public String getDob() { 
     return dob; 
    } 

    public void setDob(String dob) { 
     this.dob = dob; 
    } 

    public String getPurchaseCategoryCode() { 
     return purchaseCategoryCode; 
    } 

    public void setPurchaseCategoryCode(String purchaseCategoryCode) { 
     this.purchaseCategoryCode = purchaseCategoryCode; 
    } 

    public String getAccountType() { 
     return accountType; 
    } 

    public void setAccountType(String accountType) { 
     this.accountType = accountType; 
    } 

    public String getImsi() { 
     return imsi; 
    } 

    public void setImsi(String imsi) { 
     this.imsi = imsi; 
    } 

    public String getFirstName() { 
     return firstName; 
    } 

    public void setFirstName(String firstName) { 
     this.firstName = firstName; 
    } 

    public String getIdentificationNumber() { 
     return identificationNumber; 
    } 

    public void setIdentificationNumber(String identificationNumber) { 
     this.identificationNumber = identificationNumber; 
    } 

    @Override 
    public String toString() { 
     return "ClassPojo [lastName = " + lastName + ", additionalInfo = " 
       + additionalInfo + ", resourceURL = " + resourceURL 
       + ", status = " + status + ", msisdn = " + msisdn 
       + ", ownerType = " + ownerType + ", identificationType = " 
       + identificationType + ", onBehalfOf = " + onBehalfOf 
       + ", responseIdentifier = " + responseIdentifier 
       + ", requestIdentifier = " + requestIdentifier + ", title = " 
       + title + ", address = " + address + ", dob = " + dob 
       + ", purchaseCategoryCode = " + purchaseCategoryCode 
       + ", accountType = " + accountType + ", imsi = " + imsi 
       + ", firstName = " + firstName + ", identificationNumber = " 
       + identificationNumber + "]"; 
    } 
} 

class AdditionalInfo { 
    private String tag; 

    private String value; 

    public String getTag() { 
     return tag; 
    } 

    public void setTag(String tag) { 
     this.tag = tag; 
    } 

    public String getValue() { 
     return value; 
    } 

    public void setValue(String value) { 
     this.value = value; 
    } 

    @Override 
    public String toString() { 
     return "ClassPojo [tag = " + tag + ", value = " + value + "]"; 
    } 
} 

class Address { 
    private String line1; 

    private String line3; 

    private String country; 

    private String city; 

    private String line2; 

    public String getLine1() { 
     return line1; 
    } 

    public void setLine1(String line1) { 
     this.line1 = line1; 
    } 

    public String getLine3() { 
     return line3; 
    } 

    public void setLine3(String line3) { 
     this.line3 = line3; 
    } 

    public String getCountry() { 
     return country; 
    } 

    public void setCountry(String country) { 
     this.country = country; 
    } 

    public String getCity() { 
     return city; 
    } 

    public void setCity(String city) { 
     this.city = city; 
    } 

    public String getLine2() { 
     return line2; 
    } 

    public void setLine2(String line2) { 
     this.line2 = line2; 
    } 

    @Override 
    public String toString() { 
     return "ClassPojo [line1 = " + line1 + ", line3 = " + line3 
       + ", country = " + country + ", city = " + city + ", line2 = " 
       + line2 + "]"; 
    } 
} 
+0

これにはjacksonライブラリを使用できます。 – ManishKr

+0

Arrayの代わりにAdditionalInfoのCollectionを使用しようとしましたか?たとえば、 'List additionalInfo;'?以前Gsonライブラリを使用していましたが、それはAFAIRの問題でした。 – null

+0

AdditionalInfoクラスも提供 – Palak

答えて

1

JSONファイルに小さな間違いがあります。あなたが持っていると余分なスペース最後に、それは作ることが

"additionalInfo" 
+0

wow..thanks so much cralfaro あなたのお世話になりました:-) – Priyanka

+0

あなたは大歓迎です:) – cralfaro

1

とここ

はあなたのadditionalinfo含む(公衆にすべてのクラスを変更してください。それは役立つかもしれない。

私が知っているようにJSONパーサーの中にはプライベートクラスが好きではないので、単純に無視します。

+0

ありがとうございました – Priyanka

1

"additionalInfo " 

の変更に失敗した私は、ジャクソンの代わりGsonを使用しますが、これは好みの問題です。「itionalInfo 最初に読者(jackson/gson)が寛容であるように設定します。つまり、読者はあなたのjsonの不足しているプロパティや不明なプロパティに失敗してはいけません。 REF:TolerantReader

また、私は配列の上にコレクション(あなたのケースでのArrayList)を使用して、空のようにそれらを初期化し、nullポインタ例外を避けるためにしようとするだろう。

また、モデルにはたくさんのStringがありますので、より賢明にタイプを選択します。

、すでに@cralfaro入力中には、私が今作るつもりだった点だ。以前、このようなタイプミスを発見するためにD を、あなたのtestStringから\r\nを削除することができます。

+0

感謝の気持ちでsschrass – Priyanka

関連する問題