0

I have a List of objects Day, there are some value like:

-id
-name
-List of objects `Meal`

Meal object has:

-id
-meal number
-List of objects `Product`

Product object has:

-id
-name
-cost

I send to application List of Day, example String for:

-two `Days`
-two `Meals` for every `Day`
-three `Products` for every `Meal`

looks like:

[
    {
        "id": 1,
        "name": "firstDayName",
        "meal": [
            {
                "id": 2,
                "mealNumber": 0,
                "product": [
                    {
                        "id": 4,
                        "name": "exampleName1"
                    },
                    {
                        "id": 5,
                        "name": "exampleName2"
                    },
                    {
                        "id": 6,
                        "name": "exampleName3"
                    }
                ]
            },
            {
                "id": 7,
                "mealNumber": 1,
                "product": [
                    {
                        "id": 8,
                        "name": "exampleName4"
                    },
                    {
                        "id": 9,
                        "name": "exampleName5"
                    },
                    {
                        "id": 10,
                        "name": "exampleName6"
                    }
                ]
            }
        ]
    },
    {
        "id": 11,
        "name": "secondDayName",
        "meal": [
            {
                "id": 12,
                "mealNumber": 0,
                "product": [
                    {
                        "id": 14,
                        "name": "exampleName10"
                    },
                    {
                        "id": 15,
                        "name": "exampleName20"
                    },
                    {
                        "id": 16,
                        "name": "exampleName30"
                    }
                ]
            },
            {
                "id": 17,
                "mealNumber": 1,
                "product": [
                    {
                        "id": 18,
                        "name": "exampleName40"
                    },
                    {
                        "id": 19,
                        "name": "exampleName50"
                    },
                    {
                        "id": 20,
                        "name": "exampleName60"
                    }
                ]
            }
        ]
    }
]

How is it possible to get the same object structur from this string? I know that probably one of the ways is to use Gson but not sure how to do it.

James McCracken
  • 15,488
  • 5
  • 54
  • 62
barmi
  • 665
  • 6
  • 22
  • Do you know [link](http://www.jsonschema2pojo.org/) it will make needed classes for gson parsing – MeLean Aug 25 '16 at 18:36
  • The structure of this Json schema is not too complex. You can try out libraries such as Gson or Jackson, or others. Or you could build your own JSON library on your own. There are numerous examples on how to do this. – eddiecubed Aug 25 '16 at 18:36
  • @eddiecubed I know that I should try `Gson` but not sure how to do it, I usually use it for object lists and use `@SerializedName()` but it doesn't work for this example. Could you explain it please? – barmi Aug 25 '16 at 18:46
  • The duplicate marking on this question has loads of answers for you to read through. You can also checkout codepath https://guides.codepath.com/android/Leveraging-the-Gson-Library – eddiecubed Aug 25 '16 at 18:49
  • To be honest I don't think that this is the same example (I mean duplicate), I need a lists in lists in 'main' object – barmi Aug 25 '16 at 18:53

0 Answers0