从零开发一个完整的Android项目(五)——解析JSON字符串

本文介绍了一个针对Android平台的JSON解析工具类,通过封装简化了JSON数据的读取过程,包括获取不同类型的值、处理数组等常见操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

JSON字符串解析

接上篇。主要是包装了一下Android自带的JSON解析的API,用于HTTP通信。

代码

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

/**
 * Created by song on 2017/4/6.
 */

public class JSONParser {
    /**
     * 从json字符串取key对应的String类型value
     * @param str json字符串
     * @param key key
     * @return String类型value
     */
    public static String GetValue(String str, String key) {
        try {
            if(str != null && key != null) {
                JSONObject jsonObject = new JSONObject(str);
                return jsonObject.getString(key);
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 从json字符串取key对应的int类型value
     * @param str json字符串
     * @param key key
     * @return int类型value
     */
    public static int GetInt(String str, String key) {
        try {
            if(str != null && key != null) {
                JSONObject jsonObject = new JSONObject(str);
                return jsonObject.getInt(key);
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return -1;
    }

    /**
     * 从json字符串取key对应的long类型value
     * @param str json字符串
     * @param key key
     * @return long类型value
     */
    public static long GetLong(String str, String key) {
        try {
            if(str != null && key != null) {
                JSONObject jsonObject = new JSONObject(str);
                return jsonObject.getLong(key);
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return -1;
    }

    /**
     * 从json字符串取key对应的double类型value
     * @param str json字符串
     * @param key key
     * @return double类型value
     */
    public static double GetDouble(String str, String key) {
        try {
            if(str != null && key != null) {
                JSONObject jsonObject = new JSONObject(str);
                return jsonObject.getDouble(key);
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return -1;
    }

    /**
     * 从json字符串取key对应的JSONArray
     * @param str json字符串
     * @param key key
     * @return JSONArray
     */
    public static JSONArray GetArray(String str, String key) {
        try {
            if(str != null && key != null) {
                JSONObject jsonObject = new JSONObject(str);
                return jsonObject.getJSONArray(key);
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    /**
     * json字符串转JSONArray
     * @param str json字符串
     * @return JSONArray
     */
    public static JSONArray GetArray(String str) {
        try {
            if(str != null) {
                return new JSONArray(str);
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 从JSONArray取index对应的int类型value
     * @param jsonArray json数组
     * @param index 索引
     * @return int类型value
     */
    public static int GetIntFromJSONArray(JSONArray jsonArray, int index) {
        try {
            if(jsonArray != null) {
                return jsonArray.getInt(index);
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return -1;
    }

    /**
     * 从jsonArray取index对应的String类型value
     * @param jsonArray json数组
     * @param index 索引
     * @return String类型value
     */
    public static String GetValueFromJSONArray(JSONArray jsonArray, int index) {
        try {
            if(jsonArray != null) {
                return jsonArray.getString(index);
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 从jsonArray取所有String类型value
     * @param jsonArray json数组
     * @return String数组
     */
    public static String [] GetValuesFromJSONArray(JSONArray jsonArray) {
        try {
            if(jsonArray != null) {
                int size = jsonArray.length();
                String [] strRet = new String[size];
                for (int i = 0; i < size; ++i){
                    strRet[i] = jsonArray.getString(i);
                }
                return strRet;
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
}

说明

  • 因为很多地方都要用到,为了避免到处都是try catch,所以包了一层,方便调用。
  • 前几个Get函数好像可以合并成一个函数

public static Object GetValue(String str, String key) {
        try {
            if(str != null && key != null) {
                JSONObject jsonObject = new JSONObject(str);
                return jsonObject.get(key);
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

但是为了方便调用,还是写成各个单独的函数。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值