Gson - parsing json with field that is array or string
Asked Answered
K

4

5

I'm trying to parse some json with Gson and I have following problem:

This is my json:

[{
  "label": "Check Digit",
  "value": "W"
},
{
  "label": "Equipment",
  "value": [
    "With Standard Liftgate",
    "(-) Version Packages"
  ]
}]

This is my java class:

public class Decode {

    private String label;
    private List<String> value = new ArrayList<>();

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public List<String> getValue() {
        return value;
    }

    public void setValue(List<String> value) {
        this.value = value;
    }

}

How to make Gson to parse it and use value as Array of String always?

Khaki answered 11/6, 2016 at 23:21 Comment(3)
I parse this json string with standard: new Gson().fromJson(jsonString, WholeObjectClass.class) I think its weird situation while the same field can have different types.Skeen
Share your mapped object class code.Miceli
I added java class code. The problem is that first object in json has string instead of array of string. Json is provided by external api.Skeen
A
10

My suggestion is as follows:-

Step 1: Please make following little changes to your Decode class.

Decode.java

import java.util.ArrayList;
import java.util.List;

public class Decode {

private String label;
private List<String> values = new ArrayList<>(); // in json it is "value"

public String getLabel() {
    return label;
}

public void setLabel(String label) {
    this.label = label;
}

public List<String> getValues() {
    return values;
 }

public void setValues(List<String> values) {
    this.values = values;
 }

@Override
public String toString() {
    return "Decode [label=" + label + ", values=" + values + "]";
 }

}

Step 2: Please create following JsonDeserializer.

MyDeserializer.java

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.reflect.TypeToken;

public class MyDeserializer implements JsonDeserializer<Decode> {

@Override
public Decode deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2) throws JsonParseException {
    JsonObject decodeObj = arg0.getAsJsonObject();
    Gson gson = new Gson();
    Decode decode = gson.fromJson(arg0, Decode.class);
    List<String> values = null;
    if (decodeObj.get("value").isJsonArray()) {
        values = gson.fromJson(decodeObj.get("value"), new TypeToken<List<String>>() {
        }.getType());
    } else {
        String single = gson.fromJson(decodeObj.get("value"), String.class);
        values = new ArrayList<String>();
        values.add(single);
    }
    decode.setValues(values);
    return decode;
 }

}

Step 3: Now its time to deserialize your json as follows:

GsonMain.java

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays;

import com.google.gson.GsonBuilder;

public class GsonMain{
public static void main(String[] args) throws IOException {
    String filename = "d:/test.json"; // contains the json
    String content = new String(Files.readAllBytes(new File(filename).toPath()));

    GsonBuilder b = new GsonBuilder();
    b.registerTypeAdapter(Decode.class, new MyDeserializer());
    Decode[] array = b.create().fromJson(content, Decode[].class);
    System.out.println(Arrays.toString(array));
  }

 }
Argumentation answered 12/6, 2016 at 11:2 Comment(0)
M
1

I had the same problem, but the returned json results were more complex. I just defined those fields with uncertain types as Object type, with a little processing in the getter method.

public class POI {
    @SerializedName("address")
    private Object rawAddress;
    @SerializedName("tel")
    private Object rawTel;

    //    these two variables are used, parsed from rawXXX
    private ArrayList<String> addresses;
    private ArrayList<String> tels;

    public ArrayList<String> getAddresses() {
        if (null == addresses) {
            addresses = new ArrayList<>();
            if (rawAddress instanceof ArrayList<?>) {
                for (Object o : (ArrayList<?>) rawAddress) {
                    if (o instanceof String) {
                        addresses.add((String) o);
                    }
                }
            } else if (rawAddress instanceof String) {
                addresses.add((String) rawAddress);
            }
        }
        return addresses;
    }

    public ArrayList<String> getTels() {
        if (null == tels) {
            tels = new ArrayList<>();
            if (rawTel instanceof ArrayList<?>) {
                for (Object o : (ArrayList<?>) rawTel) {
                    if (o instanceof String) {
                        tels.add((String) o);
                    }
                }
            } else if (rawTel instanceof String) {
                tels.add((String) rawTel);
            }
        }
        return tels;
    }

    public Object getRawAddress() {
        return rawAddress;
    }

    public void setRawAddress(Object rawAddress) {
        this.rawAddress = rawAddress;
    }

    public Object getRawTel() {
        return rawTel;
    }

    public void setRawTel(Object rawTel) {
        this.rawTel = rawTel;
    }
}

Hope to help you.

Mercorr answered 6/11, 2021 at 3:43 Comment(0)
M
0

The json is not in proper format, it should be :

[{
  "label": "Check Digit",
  "value": ["W"]
},
{
  "label": "Equipment",
  "value": [
    "With Standard Liftgate",
    "(-) Version Packages"
  ]
}]

Again your parser class should be :

public class Decode {

    private String label;
    private String[] value;

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public String[] getValue() {
        return value;
    }

    public void setValue(String[] value) {
        this.value = value;
    }

}

Hope it will help :)

Mush answered 12/6, 2016 at 10:20 Comment(3)
Thanks for reply. I know that this format is weird but I receive it from external api. I'm looking for some method to map this json as it is.Skeen
For this you can try your own deserializers, refer this link : #28319973 Let me know if you still need my helpMush
I used own deserializer like in Sanjeev Saha answer. Thanks.Skeen
C
-1

the Decode solution is the cleanest, but if you don't plan on using the model for anything else than deserializing, you could just declare the field as

val myField: Any?

and then to read the value:

when (myField) {
    is List<*> -> {}
    is String -> {}
}
Corpus answered 2/2, 2023 at 15:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.