Java Json fill_dictionary(Map dictionary, JsonObject jsonObject)

Here you can find the source of fill_dictionary(Map dictionary, JsonObject jsonObject)

Description

fildictionary

License

Open Source License

Declaration

private static void fill_dictionary(Map<String, Object> dictionary, JsonObject jsonObject) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;
import java.util.HashMap;

import java.util.Map;

import javax.json.JsonArray;

import javax.json.JsonObject;

import javax.json.JsonValue;

public class Main {
    private static void fill_dictionary(Map<String, Object> dictionary, JsonObject jsonObject) {
        if (dictionary == null || jsonObject == null) {
            return;
        }//from w w  w . jav a 2 s.  c  o  m

        for (String key : jsonObject.keySet()) {
            JsonValue valueObj = (JsonValue) jsonObject.get(key);
            JsonValue.ValueType type = valueObj.getValueType();
            if (type == JsonValue.ValueType.ARRAY) {
                JsonArray arrayObj = jsonObject.getJsonArray(key);
                ArrayList<Object> array = new ArrayList<Object>();
                for (int i = 0; i < arrayObj.size(); i++) {
                    Map<String, Object> entryDictionary = new HashMap<String, Object>();
                    fill_dictionary(entryDictionary, arrayObj.getJsonObject(i));
                    array.add(entryDictionary);
                }
                dictionary.put(key, array);
            } else if (type == JsonValue.ValueType.OBJECT) {
                Map<String, Object> entryDictionary = new HashMap<String, Object>();
                fill_dictionary(entryDictionary, jsonObject.getJsonObject(key));
                dictionary.put(key, entryDictionary);
            } else if (type == JsonValue.ValueType.STRING) {
                dictionary.put(key, jsonObject.getString(key));
            } else if (type == JsonValue.ValueType.NUMBER) {
                dictionary.put(key, new Long(jsonObject.getJsonNumber(key).longValue()));
            } else if (type == JsonValue.ValueType.TRUE) {
                dictionary.put(key, Boolean.TRUE);
            } else if (type == JsonValue.ValueType.FALSE) {
                dictionary.put(key, Boolean.FALSE);
            }
        }
    }
}

Related

  1. asJsonObject(JsonValue value)
  2. asString(JsonValue value)
  3. compare(JsonValue expected, JsonValue actual, boolean strict)
  4. convertJsonValue(Object jsonValue, Class desiredType)
  5. escape(String text)
  6. fill_key_value(JsonObjectBuilder jsonObject, String key, Object value)
  7. fill_list(JsonObjectBuilder jsonObject, String key, List list)
  8. fromDictionary(Map dictionary)
  9. getAsJSONArray(Object obj)

  10. HOME | Copyright © www.java2s.com 2016