Android Json解析的封装

本文介绍了一个针对JSON数据解析的工具类ZHJsonUtil,该工具类提供了多种方法来获取JSON对象中的字符串、整数、长整型等不同类型的值,并且在数据缺失时能够返回默认值,确保了程序在面对不完整的JSON数据时仍能正常运行。
package com.ZHouseNew.android.utils;

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

/**
 * 对json解析的包装
 * @author zhoushuai
 * @version 创建时间:2014-07-10 上午11:32:45
 */
public class ZHJsonUtil {

    public static String getString(JSONObject jo, String key){
        try {
            return jo.has(key) ? jo.getString(key) : "";
        } catch (JSONException e) {
            return "";
        }
    }

    public static String getString(JSONObject jo, String key, String defaultValue){
        try {
            return jo.has(key) ? jo.getString(key) : defaultValue;
        } catch (JSONException e) {
            return defaultValue;
        }
    }



    public static int getInt(JSONObject jo, String key){
        try {
            return jo.has(key) ? jo.getInt(key) : 0;
        } catch (JSONException e) {
            return 0;
        }
    }

    public static int getInt(JSONObject jo, String key, int defaultValue){
        try {
            return jo.has(key) ? jo.getInt(key) : defaultValue;
        } catch (JSONException e) {
            return defaultValue;
        }
    }



    public static long getLong(JSONObject jo, String key){
        try {
            return jo.has(key) ? jo.getLong(key) : 0l;
        } catch (JSONException e) {
            return 0l;
        }
    }
    public static long getLong(JSONObject jo, String key, long defaultValue){
        try {
            return jo.has(key) ? jo.getLong(key) : defaultValue;
        } catch (JSONException e) {
            return defaultValue;
        }
    }


    public static boolean getBoolean(JSONObject jo, String key){
        try {
            return jo.has(key) && jo.getBoolean(key);
        } catch (JSONException e) {
            return false;
        }
    }

    public static boolean getBoolean(JSONObject jo, String key, boolean defaultValue){
        try {
            return jo.has(key) ? jo.getBoolean(key) : defaultValue;
        } catch (JSONException e) {
            return defaultValue;
        }
    }



    public static double getDouble(JSONObject jo, String key){
        try {
            return jo.has(key) ? jo.getDouble(key) : 0d;
        } catch (JSONException e) {
            return 0d;
        }
    }

    public static double getDouble(JSONObject jo, String key, double defaultValue){
        try {
            return jo.has(key) ? jo.getDouble(key) : defaultValue;
        } catch (JSONException e) {
            return defaultValue;
        }
    }



    public static JSONObject getJSONObject(JSONObject jo, String key){
        try {
            return jo.has(key) ? jo.getJSONObject(key) : null;
        } catch (JSONException e) {
            return null;
        }
    }
    public static JSONObject getJSONArray(JSONObject jo, String key, JSONObject defaultValue){
        try {
            return jo.has(key) ? jo.getJSONObject(key) : defaultValue;
        } catch (JSONException e) {
            return defaultValue;
        }
    }


    public static JSONArray getJSONArray(JSONObject jo, String key){
        try {
            return jo.has(key) ? jo.getJSONArray(key) : null;
        } catch (JSONException e) {
            return null;
        }
    }
    public static JSONArray getJSONArray(JSONObject jo, String key, JSONArray defaultValue){
        try {
            return jo.has(key) ? jo.getJSONArray(key) : defaultValue;
        } catch (JSONException e) {
            return defaultValue;
        }
    }





}
适合debug调通后使用,不会因为数据不完整而完全无法解析
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值