-3
{"data": 
  {
    "item1": {
            "name" : "Box"
            "price" : "50"

    },

    "item2": {
            "name" : "Bottle"
            "price" : "250"

    }, ..... 
    "item20": {
            "name" : "Pen"
            "price" : "100"
    }
}}

This is my API structure. I cannot create separate POJO classes for 20 items as it is not efficient . What is the best way to fetch the name and price of every item and set them to a recycler view?

Nikmaniac
  • 29
  • 6
  • Its invalid json format you can check here https://jsonlint.com/ – AskNilesh Sep 27 '18 at 13:09
  • 1
    @Khemraj i know that that's why i tell him that invalid `json format` not invalid `json` – AskNilesh Sep 27 '18 at 13:11
  • @2Dee No, please have a look at my API structure. It has object of objects. – Nikmaniac Sep 27 '18 at 13:20
  • I had a look, it doesn't have "object of objects", it's called an array and the linked question explains how to deal with arrays. I still stand by my vote, this is clearly a duplicate. This is verified by the fact that the answers you are getting are essentially proposing to use a library to do the same thing as suggested by the answers to the question I link to. BTW your API structure doesn't yield valid JSON format... – 2Dee Sep 27 '18 at 14:24

3 Answers3

0

You will have to change format of your JSON in order to parse it properly. In addition you forgot to put commas after the "name" value.

Option 1:

{
   "data":[
      {
         "name":"Box",
         "price":"50"
      },
      {
         "name":"Bottle",
         "price":"250"
      },
      {
         "name":"Pen",
         "price":"100"
      }
   ]
}

Option 2:

{
   "data":{
      "item1":{
         "name":"Box",
         "price":"50"
      },
      "item2":{
         "name":"Bottle",
         "price":"250"
      },
      "item20":{
         "name":"Pen",
         "price":"100"
      }
   }
}

Option 1 is the better choice, since you will be able to represent "data" as an array or a list in android (e.g. ArrayList<Data>).

In general i would suggest you to use a parsing library like GSON

Andre
  • 364
  • 2
  • 11
  • Yes, I changed the structure as you mentioned in option 2. But how will I overcome the inefficiency of creating 20 pojo classes for 20 items? I wanna know if I can do it using a single class. – Nikmaniac Sep 27 '18 at 13:22
  • You do not have to create a POJO class for each "item" because they all have the same structure. You can create one class and reuse it for each "item". – Andre Sep 27 '18 at 13:24
  • 1
    Why not simply use Option 1. That is better. – Rohit5k2 Sep 27 '18 at 13:33
0

Simple, just use GSON to create an object with SerializableNames to hold the json. Then simply make an arrayList of your gson compatible object.

The rest is just common adapter interactions.

Example of an API return json object that I use. It must match your json structure. enter image description here

enter image description here

The example is in Kotlin, but it's the same thing for handling json api response.

Sam
  • 5,342
  • 1
  • 23
  • 39
0

Create a simple class then do as mentioned in the image

json1

after clicking the Generate option you see the GsonFromat click then a Box will Prompt , If you are unable to see the GsonFormat then download the GsonFormat plugin. This is the simplest way by which you can easily create Pojo class.

json2

put this into the box and click ok it will automatically create the class for you and you can fetch the data.

{
   "data":[
  {
     "name":"Box",
     "price":"50"
  },
  {
     "name":"Bottle",
     "price":"250"
  },
  {
     "name":"Pen",
     "price":"100"
  }
   ]
 }
harsh
  • 337
  • 1
  • 2
  • 14