heylee.android.network.JSONParserCustom.java Source code

Java tutorial

Introduction

Here is the source code for heylee.android.network.JSONParserCustom.java

Source

package heylee.android.network;

/* Copyright (C) 2015 Heylee authors
 * 
 * selfof0800@gmail.com
 * 
 * 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.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.net.Uri;
import android.util.Base64;

public class JSONParserCustom {
    public static List<Map<String, Object>> parse(JSONArray jarr) throws JSONException {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        int len = jarr.length();
        for (int i = 0; i < len; i++) {
            JSONObject j = jarr.getJSONObject(i);
            list.add(getJsonData(j.toString()));
        }
        return list;
    }

    public static Map<String, Object> parse(JSONObject jobj) throws JSONException {
        Map<String, Object> map = new HashMap<String, Object>();
        Iterator<String> iter = jobj.keys();
        while (iter.hasNext()) {
            String key = iter.next();
            map.put(key, autoBase64Decode(jobj.getString(key)));
        }
        return map;
    }

    public static String autoBase64Decode(String CheckString) {
        String BASE64_REGEX = "^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$";
        if (CheckString.matches(BASE64_REGEX)) {
            if (CheckString == null || CheckString.isEmpty())
                return "";
            else
                return new String(Base64.decode(Uri.decode(CheckString), 0));
        }
        return CheckString;
    }

    public static HashMap<String, Object> getJsonData(String data) throws JSONException {
        JSONObject jObj = new JSONObject(data);
        Iterator<String> iKeys = jObj.keys();
        HashMap<String, Object> map = new HashMap<String, Object>();
        while (iKeys.hasNext()) {
            String key = (String) iKeys.next();
            if (jObj.get(key) instanceof JSONObject) {
                map.put(key, parse(jObj.getJSONObject(key)));
            } else if (jObj.get(key) instanceof JSONArray) {
                map.put(key, parse(jObj.getJSONArray(key)));
            } else {
                map.put(key, jObj.getString(key));
            }
        }
        return map;
    }
}