JSON jsonObject.optString()返回字符串"零" [英] JSON jsonObject.optString() returns String "null"

查看:669
本文介绍了JSON jsonObject.optString()返回字符串"零"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个Android应用程序,它使用JSON服务器通讯和我有一个奇怪的问题,当我试图分析我的JSON文件。

I'm developing an Android App which uses JSON for the server communication and I've got a weird problem when I'm trying to parse my json file.

这是我的JSON从服务器

This is my json from the server

{
    "street2": null,
    "province": null,
    "street1": null,
    "postalCode": null,
    "country": null,
    "city": null
}

我得到的价值为市通过调用字符串的城市= address.optString(城市,)我的地址JSON-对象。对于这种情况,我期待城市为空(这是optString这里是为了什么,不是吗?),但实际上它包含字符串空。因此,进一步的空值或的isEmpty,检查将返回false作为字符串包含文本。如果我称之为 address.isNull(城市)这是正确的,则返回true。只有 optString 失败。

I'm getting the value for City by calling String city = address.optString("city", "") on my address Json-object. For this situation I'm expecting cityto be empty (that's what optString is here for isn't it?) but in fact it contains the String "null". So further null- or isEmpty-checks will return false as the String contains text. If I call address.isNull("city") it returns true which is correct. Only optString fails.

我无法找到任何关于谷歌或为#1这个问题。我真的不明白它是如何发生的,因为我以为 optString 会做什么我的预期。任何人都知道发生了什么事情错在这里?

I couldn't find anything on Google or Stackoverflow for this problem. I don't really understand how it can happen as I thought optString would do exactly what I expected. Anybody knows what's going wrong here?

推荐答案

你不是一个人在运行到这个问题,并抓你的头,心想难道他们真的有这个意思?据一位AOSP问题,谷歌工程师的没有 认为这是一个错误,但他们必须与org.json实现兼容,甚至错误兼容。

You're not alone in running into this problem and scratching your head, thinking "Could they really have meant this?" According to an AOSP issue, the Google engineers did consider this a bug, but they had to be compatible with the org.json implementation, even bug-compatible.

如果你想想看,这是有道理的,因为如果同样的code,它使用相同的库在其他的Java环境中运行表现不同的Andr​​oid,在使用第三方库时,将成为主要的兼容性问题。即使意图是好的,它真正修复的错误,它会打开蠕虫的全新即可。

If you think about it, it makes sense, because if the same code which uses the same libraries run in other Java environments behaves differently in Android, there would be major compatibility problems when using 3rd party libraries. Even if the intentions were good and it truly fixed bugs, it would open up a whole new can of worms.

按照 AOSP问题

该行为是故意的;我们走出去我们的方式是错误兼容org.json。现在,多数民众赞成固定的,目前还不清楚我们是否应该解决我们的code为好。应用程序可能会依赖于这种错误行为。

The behavior is intentional; we went out of our way to be bug-compatible with org.json. Now that that's fixed, it's unclear whether we should fix our code as well. Applications may have come to rely on this buggy behavior.

如果这是造成你的悲伤,我建议你解决方法通过使用不同的机制来测试空,如json.isNull()。

If this is causing you grief, I recommend you workaround by using a different mechanism to test for null, such as json.isNull().

下面是一个简单的方法来帮助你:

Here's a simple method to help you out:

/** Return the value mapped by the given key, or {@code null} if not present or null. */
public static String optString(JSONObject json, String key)
{
    // http://code.google.com/p/android/issues/detail?id=13830
    if (json.isNull(key))
        return null;
    else
        return json.optString(key, null);
}

这篇关于JSON jsonObject.optString()返回字符串"零"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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