Json deserialization issue

Have an array when the size is 1, the received json data does NOT contain []; as

{"firstname":"tom"}

when the size is greater than 1, the data I received contain [], for example

[{"firstname":"tom"},{"firstname":"robert"}]

Currently my class contains an array property

String[] firstname;
//getter setter omit here

Code to handle this type

ObjectMapper mapper = new ObjectMapper();    
MyClass object = mapper.readValue(json, MyClass.class);

When the size is greater than 1, deserialization works. However, when the size is 1, deserialization failed.

I am currently using jackson, any solution for this problem?

I am wondering if jackson / gson or any other library can handle this?

+3
source share
5 answers

For Jackson specifically, your best bet will first bind to JsonNode or Object, for example:

Object raw = objectMapper.readValue(json, Object.class); // becomes Map, List, String etc

and then check what you have, bind again:

MyClass[] result;
if (raw instanceof List<?>) { // array
  result = objectMapper.convertValue(raw, MyClass[].class);
} else { // single object
  result = objectMapper.convertValue(raw, MyClass.class);
}

, JSON, , - , 1? - , , JSON. , .

+3

GSON. , :

public class Group{

    public Group(final List<Person> members){
        this.members = members;
    }

    private final List<Person> members;
}

public class Person{

    public Person(final String firstName, final String lastName){
        this.firstName = firstName;
        this.lastName = lastName;
    }

    private final String firstName;
    private final String lastName;
}

, Person, :

public class GroupDeserializer implements JsonDeserializer<Group>{

    @Override
    public Group deserialize(final JsonElement json,
        final Type typeOfT,
        final JsonDeserializationContext context) throws JsonParseException{
        List<Person> members;
        if(json.isJsonArray()){
            final JsonArray array = json.getAsJsonArray();
            members = new ArrayList<Person>(array.size());
            for(final JsonElement personElement : array){
                members.add(getSinglePerson(personElement, context));
            }
        } else{
            members =
                Collections.singletonList(getSinglePerson(json, context));
        }
        return new Group(members);
    }

    private Person getSinglePerson(final JsonElement element,
        final JsonDeserializationContext context){
        final JsonObject personObject = element.getAsJsonObject();
        final String firstName =
            personObject.getAsJsonPrimitive("firstname").getAsString();
        final String lastName =
            personObject.getAsJsonPrimitive("lastname").getAsString();
        return new Person(firstName, lastName);
    }

}

+2

edit: , JsonElement isJsonArray() / isJsonObject(). getAsJsonArray() getAsJsonObject().

: JsonParseException, . catch .

, , .

+1

, JSON, XML. (XML-JSON). , .

: ACCEPT_SINGLE_VALUE_AS_ARRAY.

ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);

: http://fasterxml.github.com/jackson-databind/javadoc/2.0.0/com/fasterxml/jackson/databind/DeserializationFeature.html#ACCEPT_SINGLE_VALUE_AS_ARRAY

+1

, ( , ).

JSON coding libraries usually have a "force array" parameter for such cases. Otherwise, on your client, you can check the JSON response, and if it is not an array, push the returned object into a new array.

0
source

All Articles