iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >关于JSON解析中获取不存在的key问题
  • 652
分享到

关于JSON解析中获取不存在的key问题

2024-04-02 19:04:59 652人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

目录1 . fastJSON2 . net.sf.json3 . org.json1 . fastjson 在fastjson中有些getXXX方法 , 如getString , g

1 . fastjson

在fastjson中有些getXXX方法 , 如getString , getInteger , getIntValue等 , 当调用getXXX方法时 , 如果传入的key在json中不存在 , 那调用这些方法会报错抛出异常吗 ?

首先来看代码demo

public static void main(String[] args) {
    String str = "{\"name\":\"Bob\",\"age\":\"18123\"}";
    JSONObject jsonObject = JSON.parseObject(str);
    String[] keys = { "age" ,   "score"};
    for(String key : keys){
		System.out.println(jsonObject.getString(key));
		System.out.println(jsonObject.getInteger(key));
		System.out.println(jsonObject.getIntValue(key));
    }
}

运行结果如下 : 

18123
18123
18123
null
null
0

可看到 , 对不存在的key值(score)尝试进行getXXX时 , 会返回当前类型的默认值(String返回null , Integer返回null , intValue返回0)

查看getString/getInteger源码如下 : 

public String getString(String key) {
    Object value = get(key);
    if (value == null) {
        return null;
    }
    return value.toString();
}
public Integer getInteger(String key) {
   Object value = get(key);	
   return castToInt(value);
}
  • getString就是调用get方法 , 然后判断为null , 为null则返回null , 否则调用对象的toString()方法.
  • getInteger就是相当于调用get方法 , 然后调用castToInt方法将Ojbect对象转换为Integer对象 , castToInt方法如下 : 
public static Integer castToInt(Object value) {
    if (value == null) {
        return null;
    }
 
    if (value instanceof Integer) {
        return (Integer) value;
    }
 
    if (value instanceof Number) {
        return ((Number) value).intValue();
    }
 
    if (value instanceof String) {
        String strVal = (String) value;
 
        if (strVal.length() == 0 //
            || "null".equals(strVal) //
            || "NULL".equals(strVal)) {
            return null;
        }
        
        if (strVal.indexOf(',') != 0) {
            strVal = strVal.replaceAll(",", "");
        }
 
        return Integer.parseInt(strVal);
    }
 
    if (value instanceof Boolean) {
        return ((Boolean) value).booleanValue() ? 1 : 0;
    }
 
    throw new JSONException("can not cast to int, value : " + value);
}

首先看到第一个if中进行了判断 , 如果value值为null , 则直接返回null , 那getIntValue怎么判断的呢 , getIntValue源码如下 : 

public int getIntValue(String key) {
    Object value = get(key);
 
    if (value == null) {
        return 0;
    }
 
    return castToInt(value).intValue();
}

原来在调用castToInt之前 ,就先做了一次null的判断 , 为null直接返回了0 . 

那调用get方法获取一个不存在的key时 , 为什么会返回null而不是报错呢 , 查看get源码如下

public Object get(Object key) {
    return map.get(key);
}

map是JSONObject的一个成员变量

private final Map<String, Object> map;

原来 , JSONObject先将json字符串转换为了一个map , 而map的get方法获取不存在的key时 , 返回的就是null . 

由此可以看到 , fastjson对不存在的key做了判断 , 如果没有则会返回类型的默认值 . 

2 . net.sf.json

    public static void main(String[] args) {
        String str = "{\"name\":\"Bob\",\"age\":\"18\"}";
        JSONObject jsonObject = JSONObject.fromObject(str);
//        System.out.println(jsonObject.get("gender"));//null
//        System.out.println(jsonObject.getString("gender"));//JSONObject["gender"] not found
//        System.out.println(jsonObject.getInt("age"));//18
        System.out.println(jsonObject.getInt("score"));//JSONObject["score"] is not a number
 
    }

可以看到和fastjson的处理策略不同 , 它是对不存在的key抛出一个JSONException异常 , 查看源码可以看到

    public String getString(String key) {
        this.verifyIsNull();
        Object o = this.get(key);
        if (o != null) {
            return o.toString();
        } else {
            throw new JSONException("JSONObject[" + JSONUtils.quote(key) + "] not found.");
        }
    }
    public int getInt(String key) {
        this.verifyIsNull();
        Object o = this.get(key);
        if (o != null) {
            return o instanceof Number ? ((Number)o).intValue() : (int)this.getDouble(key);
        } else {
            throw new JSONException("JSONObject[" + JSONUtils.quote(key) + "] is not a number.");
        }
    }

3 . org.json

和net.sf.json一样 , org.json对不存在的key策略还是抛出异常 . 但org.json更加严格 , 在使用get("key")时就会直接抛出异常

    public static void main(String[] args) throws Exception {
        String str = "{\"name\":\"Bob\",\"age\":\"18\"}";
        JSONObject jsonObject = new JSONObject(str);
        System.out.println(jsonObject.get("gender"));//No value for gender
        System.out.println(jsonObject.getString("gender"));//No value for gender
        System.out.println(jsonObject.getInt("age"));//18
        System.out.println(jsonObject.getInt("score"));//No value for score
    }

get方法源码如下 : 

    public Object get(String name) throws JSONException {
        Object result = nameValuePairs.get(name);
        if (result == null) {
            throw new JSONException("No value for " + name);
        }
        return result;
    }

getString源码如下 , getInt与之类似 : 

    public int getInt(String name) throws JSONException {
        Object object = get(name);
        Integer result = JSON.toInteger(object);
        if (result == null) {
            throw JSON.typeMismatch(name, object, "int");
        }
        return result;
    }

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: 关于JSON解析中获取不存在的key问题

本文链接: https://www.lsjlt.com/news/153612.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作