JsonUtil

package com.warmlight.voicepacket.utils;

import android.content.Context;
import android.content.res.AssetManager;

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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class JSONUtils {

    /**
     * 从json中获取MAP数组
     *
     * @param json
     * @return
     */
    public static ArrayList> getListMapByJson(Object json) {
        ArrayList> objs = new ArrayList>();
        JSONArray array = new JSONArray();
        // 尝试解析
        try {
            if (json == null)
                return objs;
            else if (json.getClass() == JSONArray.class)
                array = (JSONArray) json;
            else if (((String) json).length() == 0)
                return objs;
            else
                array = new JSONArray((String) json);
        } catch (JSONException e1) {
            try {
                array.put(new JSONObject((String) json));
            } catch (JSONException e2) {
//                reportError("Json无法解析", null);
            }
        }
        for (int i = 0; i < array.length(); i++) {
            HashMap map = new HashMap();
            try {
                Iterator it = array.getJSONObject(i).keys();
                while (it.hasNext()) {
                    String key = (String) it.next();
                    Object xx = array.getJSONObject(i).get(key);
                    map.put(key, xx.toString());
                }
            } catch (Exception e) {
                // 直接取数组值到map中,key为空
                try {
                    map.put("", array.get(i).toString());
                } catch (JSONException e1) {
//                    reportError("Json无法解析", null);
                }
            }
            objs.add(map);
        }
        return objs;
    }

    //本地json文件
    public static String getJson(Context context, String fileName) {

        StringBuilder stringBuilder = new StringBuilder();
        try {
            AssetManager assetManager = context.getAssets();
            BufferedReader bf = new BufferedReader(new InputStreamReader(
                    assetManager.open(fileName)));
            String line;
            while ((line = bf.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }



    public static String map2Json(Map map){
        JSONObject jsonObject = new JSONObject();
        Iterator> it = map.entrySet().iterator();
        while(it.hasNext()){
            Map.Entry entry = it.next();
            try {
                String value = "";
                if(entry.getValue() != null && !entry.getValue().equals("null"))
                    value = entry.getValue();
                jsonObject.put(entry.getKey(),value);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return jsonObject.toString();
    }

    public static JSONObject map2JsonNoToString(Map map){
        JSONObject jsonObject = new JSONObject();
        Iterator> it = map.entrySet().iterator();
        while(it.hasNext()){
            Map.Entry entry = it.next();
            try {
                String value = "";
                if(entry.getValue() != null && !entry.getValue().equals("null"))
                    value = entry.getValue();
                jsonObject.put(entry.getKey(),value);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return jsonObject;
    }

    public static String list2Json2(List data){
        JSONArray jsonArray = new JSONArray();

        for (String s : data){
            jsonArray.put(s);
        }

        return jsonArray.toString();
    }

    public static String list2Json(List> data){
        //添加JS回调代码
        JSONArray jsonArray = new JSONArray();
        for(Map map : data){
            JSONObject jsonObject = new JSONObject();
            try {
                Iterator> it = map.entrySet().iterator();
                while(it.hasNext()){
                    Map.Entry entry = it.next();
                    jsonObject.put(entry.getKey(), entry.getValue());
                }
            } catch (JSONException e) {
//                UtilLog.reportError("JSON生成异常", e);
            }
            jsonArray.put(jsonObject);
        }
        return jsonArray.toString();
    }

    public static JSONArray list2JsonArray(List> data){
        //添加JS回调代码
        JSONArray jsonArray = new JSONArray();
        for(Map map : data){
            JSONObject jsonObject = new JSONObject();
            try {
                Iterator> it = map.entrySet().iterator();
                while(it.hasNext()){
                    Map.Entry entry = it.next();
                    jsonObject.put(entry.getKey(), entry.getValue());
                }
            } catch (JSONException e) {
//                UtilLog.reportError("JSON生成异常", e);
            }
            jsonArray.put(jsonObject);
        }
        return jsonArray;
    }


}

 

你可能感兴趣的:(JsonUtil)