Example usage for com.google.gson JsonObject entrySet

List of usage examples for com.google.gson JsonObject entrySet

Introduction

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

Prototype

public Set<Map.Entry<String, JsonElement>> entrySet() 

Source Link

Document

Returns a set of members of this object.

Usage

From source file:com.mobileStore.sms.CCPRestSmsSDK.java

License:Open Source License

private static HashMap<String, Object> jsonToMap(String result) {
    HashMap<String, Object> hashMap = new HashMap<String, Object>();
    JsonParser parser = new JsonParser();
    JsonObject asJsonObject = parser.parse(result).getAsJsonObject();
    Set<Entry<String, JsonElement>> entrySet = asJsonObject.entrySet();
    HashMap<String, Object> hashMap2 = new HashMap<String, Object>();

    for (Map.Entry<String, JsonElement> m : entrySet) {
        if ("statusCode".equals(m.getKey()) || "statusMsg".equals(m.getKey()))
            hashMap.put(m.getKey(), m.getValue().getAsString());
        else {//ww  w. ja v  a 2  s .  co m
            if ("SubAccount".equals(m.getKey()) || "totalCount".equals(m.getKey()) || "token".equals(m.getKey())
                    || "downUrl".equals(m.getKey())) {
                if (!"SubAccount".equals(m.getKey()))
                    hashMap2.put(m.getKey(), m.getValue().getAsString());
                else {
                    try {
                        if ((m.getValue().toString().trim().length() <= 2)
                                && !m.getValue().toString().contains("[")) {
                            hashMap2.put(m.getKey(), m.getValue().getAsString());
                            hashMap.put("data", hashMap2);
                            break;
                        }
                        if (m.getValue().toString().contains("[]")) {
                            hashMap2.put(m.getKey(), new JsonArray());
                            hashMap.put("data", hashMap2);
                            continue;
                        }
                        JsonArray asJsonArray = parser.parse(m.getValue().toString()).getAsJsonArray();
                        ArrayList<HashMap<String, Object>> arrayList = new ArrayList<HashMap<String, Object>>();
                        for (JsonElement j : asJsonArray) {
                            Set<Entry<String, JsonElement>> entrySet2 = j.getAsJsonObject().entrySet();
                            HashMap<String, Object> hashMap3 = new HashMap<String, Object>();
                            for (Map.Entry<String, JsonElement> m2 : entrySet2) {
                                hashMap3.put(m2.getKey(), m2.getValue().getAsString());
                            }
                            arrayList.add(hashMap3);
                        }
                        hashMap2.put("SubAccount", arrayList);
                    } catch (Exception e) {
                        JsonObject asJsonObject2 = parser.parse(m.getValue().toString()).getAsJsonObject();
                        Set<Entry<String, JsonElement>> entrySet2 = asJsonObject2.entrySet();
                        HashMap<String, Object> hashMap3 = new HashMap<String, Object>();
                        for (Map.Entry<String, JsonElement> m2 : entrySet2) {
                            hashMap3.put(m2.getKey(), m2.getValue().getAsString());
                        }
                        hashMap2.put(m.getKey(), hashMap3);
                        hashMap.put("data", hashMap2);
                    }

                }
                hashMap.put("data", hashMap2);
            } else {

                JsonObject asJsonObject2 = parser.parse(m.getValue().toString()).getAsJsonObject();
                Set<Entry<String, JsonElement>> entrySet2 = asJsonObject2.entrySet();
                HashMap<String, Object> hashMap3 = new HashMap<String, Object>();
                for (Map.Entry<String, JsonElement> m2 : entrySet2) {
                    hashMap3.put(m2.getKey(), m2.getValue().getAsString());
                }
                if (hashMap3.size() != 0) {
                    hashMap2.put(m.getKey(), hashMap3);
                } else {
                    hashMap2.put(m.getKey(), m.getValue().getAsString());
                }
                hashMap.put("data", hashMap2);
            }
        }
    }
    return hashMap;
}

From source file:com.money.manager.ex.investment.prices.FixerService.java

License:Open Source License

private List<SecurityPriceModel> getPricesFromJson(JsonObject root) {
    ArrayList<SecurityPriceModel> result = new ArrayList<>();

    String dateString = root.get("date").getAsString();
    JsonObject rates = root.get("rates").getAsJsonObject();
    if (rates == null || rates.isJsonNull())
        return null;

    // prices//from   w w  w .  j a v a  2s .c  o  m
    Set<Map.Entry<String, JsonElement>> entries = rates.entrySet();
    for (Map.Entry<String, JsonElement> entry : entries) {
        SecurityPriceModel priceModel = getSecurityPriceFor(entry, dateString);
        if (priceModel == null)
            continue;

        result.add(priceModel);
    }

    return result;
}

From source file:com.networkmanagerapp.JSONParser.java

License:Apache License

/**
 * @exception FileNotFoundException, IOException, all handled internally
 * @param filename. The name of the JSON file to parse.
 * @return JSONParsingResults object containing an arraylist of JSONItems and a string array of item names
 *//*w w w.  j av  a  2 s . c o  m*/
public JSONParsingResults returnParsedData(String filename) {
    List<JSONItem> items = new ArrayList<JSONItem>();
    try {
        FileInputStream fin = NetworkManagerMainActivity.getInstance().openFileInput(filename.substring(1));
        BufferedReader reader = new BufferedReader(new InputStreamReader(fin));
        String line;
        StringBuilder builder = new StringBuilder();
        while ((line = reader.readLine()) != null) {
            builder.append(line);
        }

        JsonElement root = new JsonParser().parse(builder.toString());
        JsonArray arr = root.getAsJsonArray();
        for (int i = 0; i < arr.size(); i++) {
            JsonObject jo = arr.get(i).getAsJsonObject();
            Set<Entry<String, JsonElement>> entry = jo.entrySet();
            JSONItem item = new JSONItem();
            Iterator<Entry<String, JsonElement>> iterator = entry.iterator();
            while (iterator.hasNext()) {
                Entry<String, JsonElement> next = iterator.next();
                item.getItemData().put(next.getKey(), next.getValue().toString());
            }

            items.add(item);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    final ArrayList<String> itemArrayList = new ArrayList<String>(items.size());
    for (int i = 0; i < items.size(); i++) {
        itemArrayList.add(items.get(i).getItemData().get("name"));
    }
    String[] names = new String[itemArrayList.size()];
    itemArrayList.toArray(names);
    return new JSONParsingResults(names, items);
}

From source file:com.nextdoor.bender.operation.gelf.GelfOperation.java

License:Apache License

protected InternalEvent prefix(InternalEvent ievent) {
    DeserializedEvent devent;/*from  w ww  .j av  a 2s . c  om*/
    if ((devent = ievent.getEventObj()) == null) {
        return null;
    }

    Object payload = devent.getPayload();

    if (payload == null) {
        return null;
    }

    if (!(payload instanceof JsonObject)) {
        throw new OperationException("Payload data is not a JsonObject");
    }

    JsonObject obj = (JsonObject) payload;

    Set<Entry<String, JsonElement>> entries = obj.entrySet();
    Set<Entry<String, JsonElement>> orgEntries = new HashSet<Entry<String, JsonElement>>(entries);

    /*
     * Prefix additional fields with "_". Everything that is not a GELF field is additional.
     */
    for (Entry<String, JsonElement> entry : orgEntries) {
        String key = entry.getKey();

        if (GELF_FIELDS.contains(key)) {
            continue;
        }

        JsonElement val = entry.getValue();
        obj.remove(key);

        obj.add("_" + key, val);
    }

    return ievent;
}

From source file:com.nextdoor.bender.operation.json.key.FlattenOperation.java

License:Apache License

protected void perform(JsonObject obj) {

    Set<Entry<String, JsonElement>> entries = obj.entrySet();
    Set<Entry<String, JsonElement>> orgEntries = new HashSet<Entry<String, JsonElement>>(entries);

    for (Entry<String, JsonElement> entry : orgEntries) {
        JsonElement val = entry.getValue();

        if (val.isJsonPrimitive() || val.isJsonNull()) {
            continue;
        }//from   w  w  w .  j  a v  a  2s .c  o m

        obj.remove(entry.getKey());
        if (val.isJsonObject()) {
            perform(obj, val.getAsJsonObject(), entry.getKey());
        } else if (val.isJsonArray()) {
            perform(obj, val.getAsJsonArray(), entry.getKey());
        }
    }
}

From source file:com.nextdoor.bender.operation.json.key.FlattenOperation.java

License:Apache License

protected void perform(JsonObject obj, JsonObject nested_obj, String parent) {
    Set<Entry<String, JsonElement>> entries = nested_obj.entrySet();
    Set<Entry<String, JsonElement>> orgEntries = new HashSet<Entry<String, JsonElement>>(entries);

    for (Entry<String, JsonElement> entry : orgEntries) {
        JsonElement val = entry.getValue();
        String key = parent + separator + entry.getKey();

        if (val.isJsonObject()) {
            perform(obj, val.getAsJsonObject(), key);
        } else if (val.isJsonArray()) {
            perform(obj, val.getAsJsonArray(), key);
        } else {/*w w  w  .  ja v  a2 s.  c  om*/
            obj.add(key, val);
        }
    }
}

From source file:com.nextdoor.bender.operation.json.key.KeyNameOperation.java

License:Apache License

protected void perform(JsonObject obj) {
    Set<Entry<String, JsonElement>> entries = obj.entrySet();
    Set<Entry<String, JsonElement>> orgEntries = new HashSet<Entry<String, JsonElement>>(entries);

    for (Entry<String, JsonElement> entry : orgEntries) {

        JsonElement val = entry.getValue();
        obj.remove(entry.getKey());/*from   ww  w  .j av  a  2s  . c  o m*/
        String key = entry.getKey().toLowerCase().replaceAll("[ .]", "_");

        if (val.isJsonPrimitive()) {
            JsonPrimitive prim = val.getAsJsonPrimitive();

            if (prim.isBoolean()) {
                obj.add(key + "__bool", val);
            } else if (prim.isNumber()) {
                if (prim.toString().contains(".")) {
                    obj.add(key + "__float", val);
                } else {
                    obj.add(key + "__long", val);
                }
            } else if (prim.isString()) {
                obj.add(key + "__str", val);
            }
        } else if (val.isJsonObject()) {
            obj.add(key, val);
            perform(val.getAsJsonObject());
        } else if (val.isJsonArray()) {
            obj.add(key + "__arr", val);
        }
    }
}

From source file:com.nextdoor.bender.operation.json.key.KeyNameReplacementOperation.java

License:Apache License

protected void perform(JsonObject obj) {
    Set<Entry<String, JsonElement>> entries = obj.entrySet();
    Set<Entry<String, JsonElement>> orgEntries = new HashSet<Entry<String, JsonElement>>(entries);

    for (Entry<String, JsonElement> entry : orgEntries) {
        JsonElement val = entry.getValue();

        /*/*from w w w .j  a v  a 2s  .  c o  m*/
         * See if key matches. If it does then drop or rename. Otherwise keep recursing.
         */
        Matcher m = this.pattern.matcher(entry.getKey());
        boolean found = m.find();
        if (found) {
            /*
             * If instructed to drop then remove and continue. Otherwise remove and later rename;
             */
            obj.remove(entry.getKey());
            if (this.drop) {
                continue;
            }

            /*
             * Rename
             */
            if (val.isJsonPrimitive()) {
                obj.add(m.replaceAll(this.replacement), val);
            } else if (val.isJsonObject()) {
                obj.add(m.replaceAll(this.replacement), val);
                perform(val.getAsJsonObject());
            } else if (val.isJsonArray()) {
                JsonArray arr = val.getAsJsonArray();

                arr.forEach(item -> {
                    performOnArray(item);
                });
                obj.add(m.replaceAll(this.replacement), val);
            }
            continue;
        }

        /*
         * Keep recursing
         */
        if (val.isJsonObject()) {
            perform(val.getAsJsonObject());
        } else if (val.isJsonArray()) {
            JsonArray arr = val.getAsJsonArray();
            arr.forEach(item -> {
                performOnArray(item);
            });
        }

    }
}

From source file:com.nextdoor.bender.operation.json.key.LowerCaseKeyOperation.java

License:Apache License

protected void perform(JsonObject obj) {
    Set<Entry<String, JsonElement>> entries = obj.entrySet();
    Set<Entry<String, JsonElement>> orgEntries = new HashSet<Entry<String, JsonElement>>(entries);

    for (Entry<String, JsonElement> entry : orgEntries) {

        JsonElement val = entry.getValue();
        obj.remove(entry.getKey());/*w  w w. j  ava  2s.  co m*/
        String key = entry.getKey().toLowerCase();

        if (val.isJsonPrimitive()) {
            obj.add(key, val);
        } else if (val.isJsonObject()) {
            obj.add(key, val);
            perform(val.getAsJsonObject());
        } else if (val.isJsonArray()) {
            obj.add(key, val);

            val.getAsJsonArray().forEach(elm -> {
                if (elm.isJsonObject()) {
                    perform((JsonObject) elm);
                }
            });
        }
    }
}

From source file:com.nextdoor.bender.operation.json.value.DropArraysOperation.java

License:Apache License

protected void perform(JsonObject obj) {
    Set<Entry<String, JsonElement>> entries = obj.entrySet();
    Set<Entry<String, JsonElement>> orgEntries = new HashSet<Entry<String, JsonElement>>(entries);

    for (Entry<String, JsonElement> entry : orgEntries) {
        JsonElement val = entry.getValue();

        if (val.isJsonArray()) {
            obj.remove(entry.getKey());//w ww. ja  v  a  2  s  . co  m
        } else if (val.isJsonObject()) {
            perform(val.getAsJsonObject());
        }
    }
}