Java Json Convert jsonArrayToString(final JsonArray array)

Here you can find the source of jsonArrayToString(final JsonArray array)

Description

Parse a JsonArray into String

License

Apache License

Parameter

Parameter Description
array input array

Return

String representation of the array object

Declaration

public static String jsonArrayToString(final JsonArray array) 

Method Source Code

//package com.java2s;
/**/*from  w  ww  . j a v  a 2s. com*/
* Copyright 2013 CryptorChat
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import java.util.Iterator;

import javax.json.JsonArray;
import javax.json.JsonNumber;
import javax.json.JsonObject;
import javax.json.JsonString;
import javax.json.JsonValue;

public class Main {
    /**
     * Parse a JsonArray into String
     * @param array input array
     * @return String representation of the array object
     */
    public static String jsonArrayToString(final JsonArray array) {
        final StringBuilder builder = new StringBuilder("[");
        final Iterator<JsonValue> iterator = array.iterator();
        while (iterator.hasNext()) {
            final JsonValue current = iterator.next();
            if (!builder.toString().equalsIgnoreCase("[")) {
                builder.append(',');
            }
            builder.append(jsonValueToJsonString(current, null));
        }
        builder.append(']');
        return builder.toString();
    }

    /**
     * Parse a JsonValue into String
     * @param jsonValue input object
     * @param key key for the input object
     * @return String representation of the jsonValue object
     */
    public static String jsonValueToJsonString(final JsonValue jsonValue, final String key) {
        final StringBuilder builder = new StringBuilder();
        if (key != null) {
            builder.append('\"').append(key).append("\":");
        }
        if (jsonValue instanceof JsonString) {
            builder.append('\"').append(jsonValue.toString()).append('\"');
        } else if (jsonValue instanceof JsonNumber || jsonValue instanceof JsonObject) {
            builder.append(jsonValue.toString());
        } else if (jsonValue instanceof JsonArray) {
            builder.append(jsonArrayToString((JsonArray) jsonValue));
        } else if (jsonValue == JsonValue.TRUE) {
            builder.append("true");
        } else if (jsonValue == JsonValue.FALSE) {
            builder.append("false");
        } else if (jsonValue == JsonValue.NULL) {
            builder.append("null");
        }
        return builder.toString();
    }
}

Related

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