Json中getString()和optString()之间的区别 [英] The difference between getString() and optString() in Json

查看:553
本文介绍了Json中getString()和optString()之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JSON中 getString() optString()之间有什么区别?

What is the difference between getString() and optString() in JSON?

推荐答案

正如迭戈所提到的,检查文档(此链接现已过期 - 好的事情我们有 Wayback Machine!)在此处发布问题之前,现在你有:

As Diego mentions, it's a good idea to check the documentation (this link is now out of date - good thing we have the Wayback Machine!) before posting a question here, but now that you have:

区别在于 optString 返回空字符串( )如果您指定的密钥不存在。另一方面, getString 会抛出 JSONException 。使用 getString 如果数据丢失则是错误,如果您不确定是否会错误,请使用 optString 在那里。

The difference is that optString returns the empty string ("") if the key you specify doesn't exist. getString on the other hand throws a JSONException. Use getString if it's an error for the data to be missing, or optString if you're not sure if it will be there.

编辑:文档中的完整描述:

Full description from the documentation:


获取与键关联的可选字符串。如果没有这样的键,它返回一个空字符串。如果该值不是字符串且不为null,则将其转换为字符串。

Get an optional string associated with a key. It returns an empty string if there is no such key. If the value is not a string and is not null, then it is converted to a string.

这篇关于Json中getString()和optString()之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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