Java,Parse JSON我知道的对象是null [英] Java, Parse JSON Objects that I know are null

查看:156
本文介绍了Java,Parse JSON我知道的对象是null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组JSON对象。要解析这些数组并存储简单的数据类型值,我必须对键名进行假设并相应地存储它们。

I have an array of JSON objects. To parse these arrays and store the simply data type values, I have to make assumptions of the key names and store them accordingly.

我也知道有时键的值会为空。示例 {[promotion:null]} 我将如何解析这个?

I also know that sometimes the key's values will be null. example {["promotion":null]} how would I parse this?

如果我尝试访问key为null的键,我得到一个JSONException。现在这是有道理的,但即使我做 if(myJSObject.getString(promotion)!= null)然后我检查时仍会得到JSON异常

If I try to access a key whose value is null, I get a JSONException. Now this makes sense, but even if I do if(myJSObject.getString("promotion")!=null) then I will still get JSON exception when it checks

如何在我的代码中对空对象进行条件检查,以便我可以避免JSON异常

how would I do a conditional check in my code for null objects so that I can avoid the JSON exception

推荐答案

使用 JSONObject.optString(String key) optString(String key,String default)

编辑:...或 isNull(字符串键),当然:)

... or isNull(String key), of course :)

这篇关于Java,Parse JSON我知道的对象是null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆