在reterofit中使用GSON解析json响应

时间:2016-09-09 05:32:04

标签: android json gson retrofit

我使用改装sdk进行api通话。我有GSON sdk来解析响应。我有一些难以解析的json响应。我已经发布了下面要解析的json响应。我需要获取地址行arraylist。

我从列表中获取空数据。

{
    "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
        }
    }
}

模型类

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+"");
        }
}

2 个答案:

答案 0 :(得分:0)

我已经纠正了json,它有额外的“,”如下所示。

"StateTransition":"SearchResults",

解析http://www.jsoneditoronline.org/中的纠正json。我注意到在你的jsonObject中你还有2个带有“CAB”和“Result”键的jsonObject。在你的第二个jsonObject中你有另一个带有键“Address”的jsonObject,你在其中有“AddressLine”数组。

所以请纠正完整的pojo DineshValues。 如果您需要任何进一步的帮助,请告诉我,我将添加完整的POJO

<强>更新
解决方案1 ​​ -
正如评论中提到的,您使用jsonschema2pojo生成pojo,您可以保留那些可能只是代码并使用以下行提取地址的类

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

现在迭代addressLine并提取您需要的数据。

  

注 - 1)确保arg0是root 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+"");

        }
}

答案 1 :(得分: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);
                        }
                    }
                }
            }
        });