0

I am getting JSON Object when their is only one response(Plan) and JSON Array when their is more than one response(plan).How to handle JSON Response?

JSON ARRAY when their is more than one plan.

 {
            "ocrNumber": "0123456",
            "userName": "dddd",
            "plan": [{
                "lat": "13.1234",
                "long": "7.1234",
                "imagepath": "sd / image / demo.jpg"
            },{
                "lat": "13.1234",
                "long": "7.1234",
                "imagepath": "sd / image / demo.jpg"
            }]
        }

JSON OBJECT when their is only one plan.

 {
            "ocrNumber": "0123456",
            "userName": "dddd",
            "plan": {
                "lat": "13.1234",
                "long": "7.1234",
                "imagepath": "sd / image / demo.jpg"
            }
        }
Snofkin Suwal
  • 63
  • 1
  • 6
  • show your work. – IntelliJ Amiya Apr 16 '20 at 09:25
  • show your parsing code. besides that: thats type inconsistency against JSON standard. under defined key you should always get defined type (e.g. String, int, double) or structure (single object or array). your JSON should always contain array, eventually with single object – snachmsm Apr 16 '20 at 09:38
  • 1
    What library are you actually using to parse the JSON? If Json.Net, see [How to handle both a single item and an array for the same property using JSON.net](https://stackoverflow.com/q/18994685/10263). If you're using Jackson (Java), see [JSON Parsing Array and Single Object with same name](https://stackoverflow.com/q/45091075/10263). If Gson, see [Gson: How do I parse polymorphic values that can be either lists or strings?](https://stackoverflow.com/q/43728659/10263) Other libraries should have similar solutions involving a converter of some sort. – Brian Rogers Apr 16 '20 at 13:25
  • You have tagged your question [tag:java] and [tag:json.net] but Json.NET is a *JSON framework for .NET.*, not Java. Please [edit] your question and clarify what framework you are using to deserialize your JSON, and what you have tried so far. – dbc Apr 16 '20 at 14:45
  • you can use GSON for that, create custom class as your json response and converted using GSON lib. – Vishal Nagvadiya Apr 16 '20 at 16:31

1 Answers1

-1

add dependencies in your app level gradle file.

implementation 'com.google.code.gson:gson:2.8.5'

Create MyResponse.java

public class MyResponse {
    @SerializedName("ocrNumber")
    private String ocrNumber;   
    @SerializedName("userName")
    private String userName;

    @SerializedName("plan")
    private ArrayList<Plan> plan;

    public String getOcrNumber() {
        return ocrNumber;
    }

    public void setOcrNumber(String ocrNumber) {
        this.ocrNumber = ocrNumber;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public ArrayList<Plan> getPlan() {
        return plan;
    }

    public void setPlan(ArrayList<Plan> plan) {
        this.plan = plan;
    }
}

create Plan.java class

public class Plan {

    @SerializedName("lat")
    private String lat;
    @SerializedName("long")
    private String longStr;

    @SerializedName("imagepath")
    private String imagepath;


    public String getLat() {
        return lat;
    }

    public void setLat(String lat) {
        this.lat = lat;
    }

    public String getLongStr() {
        return longStr;
    }

    public void setLongStr(String longStr) {
        this.longStr = longStr;
    }

    public String getImagepath() {
        return imagepath;
    }

    public void setImagepath(String imagepath) {
        this.imagepath = imagepath;
    }
}

Convert your jsonString to object using Gson

Gson gson = new Gson();
MyResponse response = gson.fromJson(yourJsonString, MyResponse.class);
Vishal Nagvadiya
  • 1,178
  • 12
  • 15