Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import org.json.JSONObject;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.text.TextUtils;

public class Main {
    public static JSONArray getJsonArray(String jsonStr) {
        if (TextUtils.isEmpty(jsonStr)) {
            return null;
        }
        try {
            return new JSONArray(jsonStr);
        } catch (JSONException e) {
        }
        return null;
    }

    public static JSONArray getJsonArray(JSONArray jsonArray, int index) {
        if (jsonArray == null || jsonArray.length() <= 0 || jsonArray.length() <= index) {
            return null;
        }
        try {
            return jsonArray.getJSONArray(index);
        } catch (JSONException e) {
        }
        return null;
    }

    public static JSONArray getJsonArray(JSONObject jsonObject, String key) {
        if (jsonObject == null || TextUtils.isEmpty(key)) {
            return null;
        }
        try {
            return jsonObject.getJSONArray(key);
        } catch (JSONException e) {
        }
        return null;
    }

    public static JSONArray getJsonArray(String jsonStr, String key) {
        if (TextUtils.isEmpty(jsonStr) || TextUtils.isEmpty(key)) {
            return null;
        }
        try {
            JSONObject jsonObject = new JSONObject(jsonStr);
            return jsonObject.getJSONArray(key);
        } catch (JSONException e) {
        }
        return null;
    }
}