JSonException具有空值

编程入门 行业动态 更新时间:2024-10-27 06:20:07
本文介绍了JSonException具有空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

解析JSon时遇到很多问题,其中一个值为空值.

I'm having lots of problems when parsing a JSon and one of it's values has null value.

{ "available_from" : "2012-11-05T00:00:00Z", "available_to" : "2012-11-30T00:00:00Z", "category_id" : 2, "created_at" : "2012-11-05T14:53:39Z", ... }

例如,如果"category_id"等于null,则解析器会出现异常.我该怎么解决?

For example if "category_id" equals null I get an exception on my parser. What can I do to solve it?

希望我已经自我解释了.

Hope I've explained myself..

推荐答案

在尝试从JSONObject获取值之前,您需要检查它是否具有键以及该值是否不是"null".

Before trying to get a value from the JSONObject you need to check if it has the key and if the value is not "null".

每次您尝试访问某个键时都添加此If语句:

Add this If statement every time you try to access a certain key:

JSONObject jsonObject = new JSONObject(str); if(jsonObject.has("category_id")&&!jsonObject.isNull("category_id")){ String s = jsonObject.getString("category_id"); }

更多推荐

JSonException具有空值

本文发布于:2023-11-05 19:44:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1561675.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:JSonException

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!