2016-09-09 50 views
0

我已經使用了翻新sdk來進行api調用。我有GSON sdk解析響應。我有一些難以解析的JSON響應。我發佈了json響應,下面是解析。我需要獲取地址行arraylist。在reterofit中使用GSON解析json響應

我收到列表中的空數據。

{ 
    "QA": { 
     "CreditsUsed": 1, 
     "State": "Results" 
    }, 
    "SearchResult": { 
     "VerifyLevel": "Verified", 
     "Address": { 
      "AddressLine": [{ 
       "LineContent": "None", 
       "Label": {}, 
       "Line": "2500Kearney St" 
      }, { 
       "LineContent": "None", 
       "Label": {}, 
       "Line": {} 
      }, { 
       "LineContent": "None", 
       "Label": {}, 
       "Line": {} 
      }, { 
       "Label": "City name", 
       "Line": "Springfield" 
      }, { 
       "Label": "State code", 
       "Line": "MO" 
      }, { 
       "Label": {}, 
       "Line": "65803-5048" 
      }, { 
       "Label": "Country", 
       "Line": "UNITED STATES OF AMERICA" 
      }], 
      "DPVStatus": "DPVNotConfigured" 
     }, 
     "VerificationFlags": { 
      "StateProvinceChanged": true, 
      "PostCodeCorrected": true 
     } 
    } 
} 

Model類

public class DineshValues { 

    String Country; 
    String Search; 
    @SerializedName("LineContent") 
    String LineContent; 
    @SerializedName("Line") 
    String Line; 
    @SerializedName("AddressLine") 
    List<AddressLine> data=new ArrayList(); 

    public List<AddressLine> getData() { 
     return data; 
    } 

    public String getLineContent() { 
     return LineContent; 
    } 

    public void setLineContent(String lineContent) { 
     LineContent = lineContent; 
    } 

    public String getLine() { 
     return Line; 
    } 

    public void setLine(String line) { 
     Line = line; 
    } 

    public void setData(List<AddressLine> data) { 
     this.data = data; 
    } 
    public String getCountry() { 
     return Country; 
    } 

    public void setCountry(String country) { 
     Country = country; 
    } 

    public String getSearch() { 
     return Search; 
    } 

    public void setSearch(String search) { 
     Search = search; 
    } 
} 

AddressLine類文件

public class AddressLine { 

@SerializedName("LineContent") 
@Expose 
private String lineContent; 
@SerializedName("Label") 
@Expose 
private String label; 
@SerializedName("Line") 
@Expose 
private String line; 

/** 
* 
* @return 
* The lineContent 
*/ 
public String getLineContent() { 
return lineContent; 
} 

/** 
* 
* @param lineContent 
* The LineContent 
*/ 
public void setLineContent(String lineContent) { 
this.lineContent = lineContent; 
} 

/** 
* 
* @return 
* The label 
*/ 
public String getLabel() { 
return label; 
} 

/** 
* 
* @param label 
* The Label 
*/ 
public void setLabel(String label) { 
this.label = label; 
} 

/** 
* 
* @return 
* The line 
*/ 
public String getLine() { 
return line; 
} 

/** 
* 
* @param line 
* The Line 
*/ 
public void setLine(String line) { 
this.line = line; 
} 

這樣調用

RetrofitRest.getClient().getLogin("1111111" ,obj,new Callback<DineshValues >() { 

    @Override 
     public void success(DineshValues arg0, Response arg1) { 
      // TODO Auto-generated method stub 
      Log.e("size",arg0.getData+""); 
     } 
} 
+0

,能得到任何異常?如果是,那麼請編輯問題並添加完整的logcat報告。我想當你試圖打印arg0.getData它是打印對象地址。在AddressLine中重寫toString方法。或去爲arg0.getData.get(0).getLineContent() –

+0

09-09 05:54:53.515:E/AndroidRuntime(1411):java.lang.IndexOutOfBoundsException:無效索引0,大小爲0我得到錯誤while即時通訊使用arg0.getData.get(0).getLineContent()。 –

+0

我認爲我已經做了一些錯誤,創造pojo類的給定的響應...你可以檢查我pojo類? –

回答

0

我已經糾正它有額外的JSON 「」 如下圖所示。

"StateTransition":"SearchResults", 

http://www.jsoneditoronline.org/解析糾正後的json。我注意到,在你的jsonObject中,你還有2個jsonObject,其中包含鍵「CAB」和「結果」。在你的第二個jsonObject中,你有另一個帶有「Address」鍵的jsonObject,你在其中有「AddressLine」數組。

SO請糾正完整的pojo DineshValues。 如果您需要任何進一步的幫助,然後讓我知道,我會添加完整的POJO

UPDATE
解決方案1 ​​ -
如您用來jsonschema2pojo生成POJO評論中提到,你可以保持這些類其可以簡單的代碼,並使用下面的行提取地址

List<AddressLine> addressLine = arg0.getResult().getAddress().getAddressLine() 

現在迭代addressLine和提取所需的數據。

注 - 1)確保arg0是根類JsonObject的類類。
2)另一點是由cricket_007提到 - 每個標籤和行 屬性不是字符串,它們是對象,如{}所述。


解決方案2 -
你只能不斷從生成的類文件的AddressLine類項目,並使用下面的行提取地址。

RetrofitRest.getClient().getLogin("1111111" ,obj,new Callback<DineshValues >() { 

    @Override 
     public void success(String arg0, Response arg1) { 
      // TODO Auto-generated method stub 
      JsonObject rootJsonObject = new JsonObject(arg0); 
      JsonObject searchResultJsonObject = rootJsonObject.getJsonObject("SearchResult"); 
      JsonObject addressJsonObject = searchResultJsonObject.getJsonObject("Address") 

      Type listType = new TypeToken<List<AddressLine>>(){}.getType(); 
      List<AddressLine> addressLineList = gson.fromJson(addressJsonObject, listType); 
      //Now iterate the addressLineList to get the address 
      Log.e("size",addressLineList.getSize+""); 

     } 
} 
+0

@ cricket_007完全同意你的看法。最初我想把它添加爲評論。所以我使用了標籤。感謝您編輯它。 –

+0

我已經給出了完整的迴應...我必須得到(AddressLine)數組列表..我怎麼能得到這個...提供步驟來實現它 –

+0

給予pojo類給定的響應..我已經使用( http://www.jsonschema2pojo.org/)用於生成pojo ..但我生成了很多類,但我只需要地址值 –

0

請在下面POJO嘗試,

地址

public class Address { 

    @SerializedName("AddressLine") 
    private List<AddressLine> addressLine = new ArrayList<AddressLine>(); 
    @SerializedName("DPVStatus") 
    private String dPVStatus; 

    /** 
    * 
    * @return 
    *  The addressLine 
    */ 
    public List<AddressLine> getAddressLine() { 
     return addressLine; 
    } 

    /** 
    * 
    * @param addressLine 
    *  The AddressLine 
    */ 
    public void setAddressLine(List<AddressLine> addressLine) { 
     this.addressLine = addressLine; 
    } 

    /** 
    * 
    * @return 
    *  The dPVStatus 
    */ 
    public String getDPVStatus() { 
     return dPVStatus; 
    } 

    /** 
    * 
    * @param dPVStatus 
    *  The DPVStatus 
    */ 
    public void setDPVStatus(String dPVStatus) { 
     this.dPVStatus = dPVStatus; 
    } 

} 

AddressLine

public class AddressLine { 

    @SerializedName("LineContent") 
    private String lineContent; 
    @SerializedName("Label") 
    private String label; 
    @SerializedName("Line") 
    private String line; 

    /** 
    * 
    * @return 
    *  The lineContent 
    */ 
    public String getLineContent() { 
     return lineContent; 
    } 

    /** 
    * 
    * @param lineContent 
    *  The LineContent 
    */ 
    public void setLineContent(String lineContent) { 
     this.lineContent = lineContent; 
    } 

    /** 
    * 
    * @return 
    *  The label 
    */ 
    public String getLabel() { 
     return label; 
    } 

    /** 
    * 
    * @param label 
    *  The Label 
    */ 
    public void setLabel(String label) { 
     this.label = label; 
    } 

    /** 
    * 
    * @return 
    *  The line 
    */ 
    public String getLine() { 
     return line; 
    } 

    /** 
    * 
    * @param line 
    *  The Line 
    */ 
    public void setLine(String line) { 
     this.line = line; 
    } 

} 

數據

public class Data { 

    @SerializedName("QA") 
    private QA qA; 
    @SerializedName("SearchResult") 
    private SearchResult searchResult; 

    /** 
    * 
    * @return 
    *  The qA 
    */ 
    public QA getQA() { 
     return qA; 
    } 

    /** 
    * 
    * @param qA 
    *  The QA 
    */ 
    public void setQA(QA qA) { 
     this.qA = qA; 
    } 

    /** 
    * 
    * @return 
    *  The searchResult 
    */ 
    public SearchResult getSearchResult() { 
     return searchResult; 
    } 

    /** 
    * 
    * @param searchResult 
    *  The SearchResult 
    */ 
    public void setSearchResult(SearchResult searchResult) { 
     this.searchResult = searchResult; 
    } 

} 

QA

public class QA { 

    @SerializedName("CreditsUsed") 
    private Long creditsUsed; 
    @SerializedName("State") 
    private String state; 

    /** 
    * 
    * @return 
    *  The creditsUsed 
    */ 
    public Long getCreditsUsed() { 
     return creditsUsed; 
    } 

    /** 
    * 
    * @param creditsUsed 
    *  The CreditsUsed 
    */ 
    public void setCreditsUsed(Long creditsUsed) { 
     this.creditsUsed = creditsUsed; 
    } 

    /** 
    * 
    * @return 
    *  The state 
    */ 
    public String getState() { 
     return state; 
    } 

    /** 
    * 
    * @param state 
    *  The State 
    */ 
    public void setState(String state) { 
     this.state = state; 
    } 

} 

信息搜索結果

public class SearchResult { 

    @SerializedName("VerifyLevel") 
    private String verifyLevel; 
    @SerializedName("Address") 
    private Address address; 
    @SerializedName("VerificationFlags") 
    private VerificationFlags verificationFlags; 

    /** 
    * 
    * @return 
    *  The verifyLevel 
    */ 
    public String getVerifyLevel() { 
     return verifyLevel; 
    } 

    /** 
    * 
    * @param verifyLevel 
    *  The VerifyLevel 
    */ 
    public void setVerifyLevel(String verifyLevel) { 
     this.verifyLevel = verifyLevel; 
    } 

    /** 
    * 
    * @return 
    *  The address 
    */ 
    public Address getAddress() { 
     return address; 
    } 

    /** 
    * 
    * @param address 
    *  The Address 
    */ 
    public void setAddress(Address address) { 
     this.address = address; 
    } 

    /** 
    * 
    * @return 
    *  The verificationFlags 
    */ 
    public VerificationFlags getVerificationFlags() { 
     return verificationFlags; 
    } 

    /** 
    * 
    * @param verificationFlags 
    *  The VerificationFlags 
    */ 
    public void setVerificationFlags(VerificationFlags verificationFlags) { 
     this.verificationFlags = verificationFlags; 
    } 

} 

VerificationFlags

public class VerificationFlags { 

    @SerializedName("StateProvinceChanged") 
    private Boolean stateProvinceChanged; 
    @SerializedName("PostCodeCorrected") 
    private Boolean postCodeCorrected; 

    /** 
    * 
    * @return 
    *  The stateProvinceChanged 
    */ 
    public Boolean getStateProvinceChanged() { 
     return stateProvinceChanged; 
    } 

    /** 
    * 
    * @param stateProvinceChanged 
    *  The StateProvinceChanged 
    */ 
    public void setStateProvinceChanged(Boolean stateProvinceChanged) { 
     this.stateProvinceChanged = stateProvinceChanged; 
    } 

    /** 
    * 
    * @return 
    *  The postCodeCorrected 
    */ 
    public Boolean getPostCodeCorrected() { 
     return postCodeCorrected; 
    } 

    /** 
    * 
    * @param postCodeCorrected 
    *  The PostCodeCorrected 
    */ 
    public void setPostCodeCorrected(Boolean postCodeCorrected) { 
     this.postCodeCorrected = postCodeCorrected; 
    } 

} 

改造呼叫

RetrofitRest.getClient().getLogin("your text", obj, new Callback<Data>() { 
      @Override 
      public void success(Data data, Response response) { 
       SearchResult searchResult = data.getSearchResult(); 
       if (searchResult != null) { 
        Address address = searchResult.getAddress(); 
        if (address != null && address.getAddressLine() != null) { 
         for (AddressLine addressLine : address.getAddressLine()) { 
          Log.d("TAG", "addressLine" + addressLine); 
         } 
        } 
       } 
      } 
     }); 
+0

我嘗試所有這些東西..但我仍然在數據對象上得到空...公共無效成功(數據數據,響應響應){ –

+0

String Country; \t字符串搜索; \t public String getCountry(){ \t \t return Country; \t} \t公共無效setCountry(字符串國家){ \t \t國家=國家; \t} \t public String getSearch(){ \t \t return Search; \t} \t公共無效setSearch(字符串搜索){ \t \t搜索=搜索; Data obj = new Data(); \t \t obj.setCountry(「NewJersey」); \t \t obj.setSearch(「@ 2500 East Kearney Street,@,@,@ Springfield,@ MO,@ 65803」); –

+0

我在Data類中添加了這些行,用於將post參數傳遞給reterofit –