Example usage for com.google.gson.internal LinkedTreeMap entrySet

List of usage examples for com.google.gson.internal LinkedTreeMap entrySet

Introduction

In this page you can find the example usage for com.google.gson.internal LinkedTreeMap entrySet.

Prototype

EntrySet entrySet

To view the source code for com.google.gson.internal LinkedTreeMap entrySet.

Click Source Link

Usage

From source file:org.cgiar.ccafs.marlo.utils.AutoSaveReader.java

License:Open Source License

private HashMap<String, Object> getOneToMany(String json) {
    Gson gson = new Gson();
    JsonObject jobj = gson.fromJson(json, JsonObject.class);
    HashMap<String, Object> jsonNew = new HashMap<>();
    LinkedTreeMap<String, Object> result = gson.fromJson(jobj, LinkedTreeMap.class);
    Set<String> listNames = new HashSet<>();
    HashMap<String, Object> onetoMany = new HashMap<>();
    for (Map.Entry<String, Object> entry : result.entrySet()) {
        String key = entry.getKey();
        String keys[] = key.split("\\.");
        String keyList = keys[0];

        listNames.add(keyList);//w ww .j av a2 s . c  o  m
    }
    for (String name : listNames) {
        HashMap<String, Object> relation = new HashMap<>();
        for (Map.Entry<String, Object> entry : result.entrySet()) {
            String key = entry.getKey();
            String keys[] = key.split("\\.");
            String keyList = keys[0];
            if (keys.length >= 3) {
                onetoMany = new HashMap<>();
                onetoMany.put(key.replaceAll(keyList + "\\." + keys[1] + "\\.", ""), entry.getValue());
                relation.put(keys[1], this.convertJSONFormat(gson.toJson(onetoMany)));
            } else {
                if (keyList.equals(name)) {

                    relation.put(keys[1], entry.getValue());
                    jobj.remove(key);

                }
            }
        }

        jsonNew.put(name, relation);

    }
    return jsonNew;
}