Java Json Convert toList(JsonArray array)

Here you can find the source of toList(JsonArray array)

Description

to List

License

Open Source License

Declaration

public static List<Object> toList(JsonArray array) throws JsonException 

Method Source Code

//package com.java2s;
/*// www . jav a2 s. com
 * Copyright 2016 Red Hat, Inc. and/or its affiliates.
 *
 * Licensed under the Eclipse Public License version 1.0, available at
 * http://www.eclipse.org/legal/epl-v10.html
 */

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.json.JsonArray;
import javax.json.JsonException;
import javax.json.JsonObject;

public class Main {
    public static List<Object> toList(JsonArray array) throws JsonException {
        List<Object> list = new ArrayList<Object>();
        for (int i = 0; i < array.size(); i++) {
            Object value = array.get(i);
            if (value instanceof JsonArray) {
                value = toList((JsonArray) value);
            } else if (value instanceof JsonObject) {
                value = toMap((JsonObject) value);
            }
            list.add(value);
        }
        return list;
    }

    public static Map<String, Object> toMap(JsonObject object) throws JsonException {
        Map<String, Object> map = new HashMap<String, Object>();

        Iterator<String> keysItr = object.keySet().iterator();
        while (keysItr.hasNext()) {
            String key = keysItr.next();
            Object value = object.get(key);

            if (value instanceof JsonArray) {
                value = toList((JsonArray) value);
            } else if (value instanceof JsonObject) {
                value = toMap((JsonObject) value);
            }
            map.put(key, value);
        }
        return map;
    }
}

Related

  1. jsonArrayToBooleanList(final JsonArray arr)
  2. jsonArrayToString(final JsonArray array)
  3. toIntArray(JsonArray jsonArray)
  4. toJsonString(JsonObject json)
  5. toString(JsonValue json)
  6. toStringArray(JsonArray jsa)