Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Skip root element while deserializing json

Tags:

java

json

gson

How should I deserialize following JSON to skip root element and parse just the inner part of this JSON. I'd like to avoid creating additional, 3rd class Root, which would include only MapWrapper field.

{
    "root": {
        "language": "en",
        "map": {
            "k1": {
                "name": "n1",
            },
            "k2": {
                "name": "n2",
            }
        }
    }
}

So I'd like to have only these two classes:

class MapWrapper {
    private String language;
    private Map<String, MyMapEntry> map;
}

class MyMapEntry {
    String name;
}
like image 936
Mateusz Chromiński Avatar asked Aug 17 '12 10:08

Mateusz Chromiński


People also ask

Does JSON require a root element?

Root element JSON may not start with a root element. JSON supports datatype including integer and strings, JSON also supports array. XML does not provide any data type so needs to be parsed into particular datatype.

What is a root element in JSON?

According to the modified Backus-Naur-Form on the right side pane of http://json.org/ the root element of a JSON data structure can be any of these seven types/values: Object Array String Number true false null.

What is serializing and deserializing JSON?

JSON is a format that encodes objects in a string. Serialization means to convert an object into that string, and deserialization is its inverse operation (convert string -> object).

Is JSON deserialization slow?

The expected latency when downloading anything from a server to a client should increase as the size of the file increases. Simply, the bigger the file the longer it takes to download.


2 Answers

you can use GSON Library for this.

Below code will solve your problem.

public class ConvertJsonToObject {

    private static Gson gson = new GsonBuilder().create();

    public static final <T> T getFromJSON(String json, Class<T> clazz) {
        return gson.fromJson(json, clazz);
    }

    public static final <T> String toJSON(T clazz) {
        return gson.toJson(clazz);
    }
}

String json; // your jsonString
Map<String,Object> r = ConvertJsonToObject.getFromJSON(json,Map.class);
String innerJson = ConvertJsonToObject.toJson(r.get("root"));
MapWrapper _r = ConvertJsonToObject.getFromJSON(innerJson,MapWrapper.class);
like image 172
Byter Avatar answered Oct 01 '22 02:10

Byter


Consider the following JSON:

{"authorization":{"username":"userabc", "password":"passabc"}}

The DTO for this JSON without the root element

public class Authorization {
    private String username;
    private String password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    // Add a container for the root element
    public static class Container {
        public Authorization authorization;
    }
}

Convert from/to JSON using the following methods (you can either keep this within DTO or some other help class)

public String toJson(Authorization authorization) {
    Gson gson = new Gson();
    Authorization.Container container = new Authorization.Container();
    container.authorization = authorization;
    return gson.toJson(container);
}

public Authorization fromJson(String json) {
    Gson gson = new Gson();
    Authorization.Container container = gson.fromJson(json, Authorization.Container.class);
    return container.authorization;
}
like image 28
yottabrain Avatar answered Oct 01 '22 03:10

yottabrain