-4

My JSON:

[
  {
    "productid": "3",
    "name": "Potato",
    "price": "15",
    "quantity": "",
    "stock": "0",
    "description": "Fresh and good",
    "image1": "upload/Potato1.jpg",
    "image2": "upload/Potato2.jpg",
    "image3": "upload/Potato3.jpg",
    "status": "Out of Stock",
    "type": "Vegetables"
  },
  {
    "productid": "5",
    "name": "Tomato",
    "price": "25",
    "quantity": "",
    "stock": "45",
    "description": "Fresh and good",
    "image1": "upload/Tomato1.jpg",
    "image2": "upload/Tomato2.jpg",
    "image3": "upload/Tomato3.jpg",
    "status": "Avaiable",
    "type": "Vegetables"
  },
  {
    "productid": "4",
    "name": "Onion",
    "price": "30",
    "quantity": "",
    "stock": "50",
    "description": "Fresh and good",
    "image1": "upload/Onion1.jpg",
    "image2": "upload/Onion2.jpg",
    "image3": "upload/Onion3.jpg",
    "status": "Avaiable",
    "type": "Vegetables",
    "wishlistid": "43",
    "userid": "10",
    "flag": "1"
  },
  {
    "productid": "6",
    "name": "Carrot",
    "price": "20",
    "quantity": "",
    "stock": "50",
    "description": "Fresh and good",
    "image1": "upload/Carrot1.jpg",
    "image2": "upload/Carrot2.jpg",
    "image3": "upload/Carrot3.jpg",
    "status": "Avaiable",
    "type": "Vegetables",
    "wishlistid": "47",
    "userid": "10",
    "flag": "1"
  }
]

In this first 2 json object have few column missing like userid and wishlistid and flag and remaining are having the userid, wishlistid and flag.

How can I read this with retrofit library .. because it gives me nothing as there is no userid column found in the first 2 objects.

Hash
  • 4,647
  • 5
  • 21
  • 39
Dimple patel
  • 152
  • 1
  • 13

1 Answers1

0

You can parse json tow way one is manual or other using parser like Gson, Also better for both you create pojo class which help you.

String yourJsonString=jsondata;
        JSONArray  jsonArray= null;
        try {
            jsonArray = new JSONArray(yourJsonString);
            if(jsonArray!=null && jsonArray.length()>0){

                //Here you can parse tow way one is manual or other one using gson

                //1.Manual one
                for(int pos=0;pos<jsonArray.length();pos++){
                    JSONObject jsonObject=jsonArray.getJSONObject(pos)
                    //Here you can parse your object tow one is pass all value by manual
                    //like YourModelObject.setMethod(jsonObject.getString("keyName");

                    //Or other one is create model class and parse data using gson like following
                    /*CustomModel modelObj= new Gson().fromJson(jsonObject, new TypeToken<LocationHoModel>() {
                    }.getType());*/


                }

                //2.Using Gson
                /*ArrayList<CustomModel> customModelArrayList = new Gson().fromJson(jsonArray, new TypeToken<ArrayList<CustomModel>>() {
                }.getType());*/

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
Dhaval Solanki
  • 4,589
  • 1
  • 23
  • 39
  • You can read the JSON with json.getOptString() instead of json.getString() method . getOptString() method return null if no attribute found for the key name and if we write getString() then it throw the error .. So completed the task . Solved my query – Dimple patel May 16 '18 at 03:43
  • Good, And Thanks for providing information @Dimple. – Dhaval Solanki May 16 '18 at 04:10