0

I am using GSON Library to parse my JSON tag.

some of the tags are expected to hold string values (not arrays). the problem is, sometimes the element is empty [] and when it does that the console gives me this error

expected String but was BEGIIN ARRAY.

The following is the ideal case for my JSON

   {
     "internet": "600.00",
    "internet_remarks": "Fibre 1gbps",
    }

but sometimes it becomes

        {
        "internet": "600.00",
        "internet_remarks": [],
        }

My parsing code is as follows:

GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.create();
MainContainer mainContainer = gson.fromJson(obj, MainContainer.class);    

while in my class is as follows the varailbas are defined as follows

private String internet;
private String internet_remarks;

My question is what changes should i make so that the variable accommodate the empty array []

3 Answers 3

4

If you don't control the source of the (poorly designed) JSON and can't fix it, then you're going to need to write a custom deserializer that constructs your MainContainer object.

See: How do I write a custom deserializer? here on SO and/or the information in the Gson User's guide

If the only time that field is an array type is when it's an empty array, the easiest approach I can think of is simply inspecting the returned JSON and if it's an array, remove it. Then deserialize to your MainContainer. Gson silently ignores any missing elements in the JSON and internet_remarks will be null in your MainContainer.

class MyDeserialier implements JsonDeserializer<MainContainer>
{
    @Override
    public MainContainer deserialize(JsonElement je, Type type, 
                                     JsonDeserializationContext jdc) 
                                      throws JsonParseException
    {
        JsonObject obj = je.getAsJsonObject();
        if (obj.get("internet_remarks").isJsonArray())
        {
            obj.remove("internet_remarks");
        }

        return new Gson().fromJson(obj, MainContainer.class):
    }
}

If that's not actually the case and that array might not be empty, you'll need to add the logic to deal with that and convert it to a String if that's what you really want.

2
  • Yeah the only time the field is an array is when it is empty. I try to detect the "[]" charcter and the string and replace it with "" ...
    – didiFaridi
    Jul 2, 2013 at 21:18
  • The above will do the trick then. It'll just be null rather than an empty String which IMHO is better anyway. Jul 2, 2013 at 21:20
0

In your case:

The gson parser is confused when you are asking it to parse an array into string.

So, you can change the internet_remarks into an array of string.

Its always a safe approach to use annonations in the container class.

Extras:

When ever you are working with json, these two tools will be handy.

1.OnlineJsonEditor.

2.JsonGen

5
  • yes. I tried to do that, but then when the tag contins string .. it gives me this error expected BEGIIN ARRAY but was String
    – didiFaridi
    Jun 21, 2013 at 4:24
  • 1
    You can change the response to be always array.
    – amalBit
    Jun 21, 2013 at 4:25
  • How would one do that? should I do that at the class. any sample code will be helpful
    – didiFaridi
    Jun 21, 2013 at 4:27
  • There is inconsistencies with your json code. Please check that. And from where are you getting your json?
    – amalBit
    Jun 21, 2013 at 4:35
  • Humm. so you are suggesting that I should make sure that my JSON have a single type?
    – didiFaridi
    Jun 21, 2013 at 4:39
0

I would suggest declare your second attribute as an array. i.e.

private String[] internet_remarks;

When you have a single string, still put it in the array. eg:

{
     "internet": "600.00",
     "internet_remarks": ["Fibre 1gbps"]
    }

When it's empty,

 {
     "internet": "600.00",
     "internet_remarks": []
    }

So your json will always be consistent.

Also, I noted that you're not really making use of the GsonBuilder. You could just create the Gson instance with a constructor than a builder.

Gson gson = new Gson();
2
  • yup, but i don't have control over the JSON. Is there anyway where i could change the data type that i get before assigning it to my attribute?
    – didiFaridi
    Jun 21, 2013 at 5:03
  • That's a really badly designed json object then! :P Hmmm, I'm still pondering about how to make it work without changing the json
    – Niks
    Jun 21, 2013 at 7:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.