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<Map<String, String>> getListMapByJson(Object json) {
        ArrayList<Map<String, String>> objs = new ArrayList<Map<String, String>>();
        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<String, String> map = new HashMap<String, String>();
            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<String,String> map){
        JSONObject jsonObject = new JSONObject();
        Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
        while(it.hasNext()){
            Map.Entry<String, String> 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<String,String> map){
        JSONObject jsonObject = new JSONObject();
        Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
        while(it.hasNext()){
            Map.Entry<String, String> 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<String> data){
        JSONArray jsonArray = new JSONArray();

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

        return jsonArray.toString();
    }

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


}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值