1

I am getting response as JSON Object or JSON Array in same api key.I am using Gson to parse data but i am getting and error as above while parsing json object.But it is okay with JSON Array.

Response with JSON Object

    {
      "Code": "000",
      "Message": "Success",
      "RenewalPlans": {
        "RenewalPlan": {
          "PlanId": "123",
          "PlanName": "super plan",
          "PlanAmount": "6102"
        }
      }
    }

//json array
{
  "Code": "000",
  "Message": "Success",
  "RenewalPlans": {
    "RenewalPlan": [
      {
        "PlanId": "456",
        "PlanName": "super",
        "PlanAmount": "6102"
      },
      {
        "PlanId": "123",
        "PlanName": "Power plan",
        "PlanAmount": "123"
      }
    ]
  }
}

Using Gson to parse data

CustomerdetailsResponse customerDetailsResponse = new Gson().fromJson(new Gson().toJson(response), CustomerdetailsResponse.class);

CustomerdetailsResponse class

public class CustomerdetailsResponse implements Serializable {
    @SerializedName("Code")
    @Expose
    private String mCode;
         

    @SerializedName("Message")
    @Expose
    private String mMessage

    @SerializedName("RenewalPlans")
    @Expose
    private RenewalPlans mRenewalPlans;
   
    public String getCode() {
        return mCode;
    }

    public void setCode(String code) {
        mCode = code;
    }

  
  

    public String getMessage() {
        return mMessage;
    }

    public void setMessage(String message) {
        mMessage = message;
    }

  

  

    public RenewalPlans getRenewalPlans() {
        return mRenewalPlans;
    }

    public void setRenewalPlans(RenewalPlans renewalPlans) {
        mRenewalPlans = renewalPlans;
    }

  

 

}

RenewalPlans class

public class RenewalPlans implements Serializable {
    @SerializedName("RenewalPlan")
    @Expose

    private ArrayList<RenewalPlan> mRenewalPlan;

    public ArrayList<RenewalPlan> getRenewalPlan() {
        return mRenewalPlan;
    }

    public void setRenewalPlan(ArrayList<RenewalPlan> renewalPlan) {
        mRenewalPlan = renewalPlan;
    }

RenewalPlan class

public class RenewalPlan implements Serializable {
    @SerializedName("PlanAmount")
    @Expose

    private String mPlanAmount;
    @SerializedName("PlanId")
    @Expose

    private String mPlanId;
    @SerializedName("PlanName")
    @Expose

    private String mPlanName;

    public String getPlanAmount() {
        return mPlanAmount;
    }

    public void setPlanAmount(String planAmount) {
        mPlanAmount = planAmount;
    }

    public String getPlanId() {
        return mPlanId;
    }

    public void setPlanId(String planId) {
        mPlanId = planId;
    }

    public String getPlanName() {
        return mPlanName;
    }

    public void setPlanName(String planName) {
        mPlanName = planName;
    }

}
3
  • Plz share CustomerdetailsResponse class Commented Jul 26, 2020 at 13:34
  • can you review my question as i post my required classes. @ShaluTD Commented Jul 26, 2020 at 13:59
  • The problem comes from service design, it should provide responses in the same way (e.g. always returning the RenewalPlans as json array). I think using gson you won't be able to parse such json strings. Commented Jul 26, 2020 at 14:17

1 Answer 1

1

Your RenewalPlans class type of property mRenewalPlan should be object instaead of ArrayList. Object can hold both. Later you can again parse object to your requirement.

Sign up to request clarification or add additional context in comments.

1 Comment

Making object and then Parsing did the trick.Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.