0

I have a json object that inside can have a property that can be array or a boolean value. So my question is how can I parse this json without having issues while parsing it?

Here is what im talking about:

 {
        "offerId": "24",
        "image1": "1452678081_56961bc195cf8.jpg",
        "image2": "1452678081_56961bc195f38.jpg",
        "image3": "1452678081_56961bc196124.jpg",
        "featured": false,
        "hide_button": false,
        "categoryId": "1",
        "locations": [
            {
                "address": "Problemveien 13, 0313 Oslo",
                "long": "10.722052800000029",
                "latitude": "59.9409999"
            },
            {
                "address": "Akersgata 68, 0180 Oslo",
                "long": "10.744572199999993",
                "latitude": "59.9174608"
            },
            {
                "address": "Trimveien 4, 0372 Oslo",
                "long": "10.724201200000039",
                "latitude": "59.9466293"
            },
            {
                "address": "Sandakerveien 116, 0484 Oslo",
                "long": "10.769448300000022",
                "latitude": "59.9483484"
            },
            {
                "address": "Vulkan 15, 0178 Oslo",
                "long": "10.75169040000003",
                "latitude": "59.9223593"
            }
        ]
    }

and this is how the object looks when it doesnt have the locations array inside:

{
        "offerId": "25",
        "image1": "1452678113_56961be1d6774.jpg",
        "image2": "1452678113_56961be1d694f.jpg",
        "image3": "1452678113_56961be1d6ae0.jpg",
        "featured": false,
        "hide_button": false,
        "categoryId": "1",
        "locations": false
    }

So once again, how can I parse the locations element because it can be a boolean or a array? Is there a way to parse this using google gson?

Thanks

Community
  • 1
  • 1
DPE
  • 218
  • 5
  • 15
  • That doesnt really sound valid as a concept. So not sure why they have done that. They should have left locations as an empty array. – IAmGroot Jun 01 '16 at 21:00
  • @Doomsknight yea i know but is there a solution for this? – DPE Jun 01 '16 at 21:05
  • to my knowledge no. not with gson. As it maps to a defined object, which would have to be 1 not 2 types – IAmGroot Jun 01 '16 at 21:41
  • possible duplicate: http://stackoverflow.com/questions/16992891/gson-deserialize-json-with-varying-value-types – Emma Jun 02 '16 at 00:14

2 Answers2

0

I don't understand what you want, however try this. It can parse any json objects, but you have to check type and cast when get locations.

class JavaBean {
    Object locations;
}
sakony
  • 88
  • 1
  • 9
0

Create classes like below

public class Location {
    String address;
    String long; // change the name of variable here and in json as well.
    String latitude;
}

public class MyClass {
    String offerId;
    String image1;
    String image2;
    String image3;
    boolean featured;
    boolean hide_button;
    String categoryId;
    List<Location> locations;
}

And you will be able to parse with Gson.

Note

  1. I suggest you to rename the property long in your location object as long is a keyword in java and the ide will throw error.

  2. I also suggest you to make the location array as empty json array instead of sending false or Gson will throw an exception.

Hope this will help you.

SripadRaj
  • 1,687
  • 2
  • 22
  • 33