Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import org.json.JSONArray;

import java.util.Arrays;
import java.util.Collections;

import java.util.List;

public class Main {
    /**
     * @return An ArrayList of the elements of array converted to strings, or the empty list if
     * array is null. Empty strings in the input array are preserved.
     */
    public static List<String> getStringList(JSONArray array) {
        String[] stringArray = getStringArray(array);
        if (stringArray == null) {
            return Collections.emptyList();
        }

        return Arrays.asList(stringArray);
    }

    /**
     * Returns a primitive array of the JSONArray's string values or an empty array if the provided
     * array is empty. If an entry is invalid or null, the corresponding index in the returned array
     * will be <code>null</code>.
     */
    public static String[] getStringArray(JSONArray array) {
        if (array == null) {
            return new String[0];
        }

        int len = array.length();
        String[] list = new String[len];
        for (int i = 0; i < len; i++) {
            String obj = array.optString(i, null);
            if (obj != null) {
                list[i] = obj;
            }
        }
        return list;
    }
}