【问题标题】:Json object is null when trying to parse尝试解析时 Json 对象为空
【发布时间】:2017-08-17 08:51:34
【问题描述】:

我有以下从我的服务器返回的 JSON 字符串(如下)。 我可以成功提取“OK”的resultCode 值。

我遇到的问题是提取user 对象,它返回null。 我看过下面关于 SO 的帖子,我相信我做得对。

json example that i have tried

谁能指点我正确的方向?

{
    "resultCode":"OK",
    "configs":[
        {
            "configID":"1",
            "configName":"Data Limit",
            "configValue":"5000000"
        },
        {
            "configID":"2",
            "configName":"Connectivity Settings Frequency",
            "configValue":"55"
        },
        {
            "configID":"3",
            "configName":"User Tracking Frequency",
            "configValue":"56"
        },
        {
            "configID":"4",
            "configName":"Data Monitoring Frequency",
            "configValue":"57"
        },
        {
            "configID":"5",
            "configName":"User Device Frequency",
            "configValue":"58"
        },
        {
            "configID":"6",
            "configName":"Telephony Settings Frequency",
            "configValue":"59"
        },
        {
            "configID":"7",
            "configName":"Restrictions Settings Frequency",
            "configValue":"60"
        },
        {
            "configID":"8",
            "configName":"DateTime Settings Frequency",
            "configValue":"61"
        },
        {
            "configID":"9",
            "configName":"Advanced Settings Frequency",
            "configValue":"62"
        }
    ],
    "companyInfo":{
        "companyID":"1",
        "companyName":"Test Company",
        "webServiceGuid":"11E0662D-6672-406C-977A-4BE9124B3E35",
        "url":"http:\/\/xxx.yourofficeanywhere.co.uk\/",
        "portNumber":"51000"
    },
    "user":{
        "userID":"8",
        "samsungApiKey":"ABC",
        "surname":"Womersley"
    }

}

.

JSONObject mainObject = new JSONObject(result);
Log.e(TAG, "mainObject = " + mainObject);

resultCode = mainObject.get("resultCode").toString();
Log.e(TAG, "resultCode = " + resultCode);
cv.put("resultcode", resultCode);

JSONObject user = mainObject.getJSONObject("user");
Log.e(TAG, "user object = " + user);
String userID = user.getString("userID");
Log.e(TAG, "userID = " + userID);
cv.put("userid", userID);

.

[编辑1] @flotto

JSONObject mainObject = new JSONObject(result);
Log.e(TAG, "mainObject = " + mainObject);
JSONArray arr = mainObject.names();

for(int i = 0; i < arr.length(); i++) {
    String name = arr.getString(i).toString();
    Log.e(TAG, "name = " + name);
}

结果:

03-24 12:19:34.702 2893-4899/d.co.uk.d E/WebService: name = resultCode
03-24 12:19:34.702 2893-4899/d.co.uk.d E/WebService: name = configs
03-24 12:19:34.702 2893-4899/d.co.uk.d E/WebService: name = companyInfo
03-24 12:19:34.702 2893-4899/d.co.uk.d E/WebService: name = user

【问题讨论】:

  • 您确定用户对象存在于服务器答案中吗?在函数的开头打印出完整的 json 对象
  • @flotto 嗨,我不确定你的意思。完整的 Json 发布在上面,用户对象在服务器发回的结果中。
  • 你也可以检查 userobj.has("userID") 然后获取 userid
  • @turtleboy 你的代码似乎是正确的,这就是为什么我要求额外的调试输出。
  • @flotto 嗨,我在 EDIT1 中放置了一些日志输出。谢谢

标签: android json


【解决方案1】:

我向所有人致歉,我没有实例化我的 Contentsvalue,因此在尝试向其添加 resulCode 时它崩溃了。我忽略了一个简单的错误。谢谢大家的帮助

【讨论】:

  • 谢谢你的反对,你真是个好人!!......显然你在编程和潜在的生活中没有犯任何错误哈哈
【解决方案2】:

在阅读 resultCode 时调试代码。在那一刻,“mainObject”应该包含用户 JSON 对象。如果为 null,则您的服务器未向您发送您正在等待的正确 JSON。

【讨论】:

    猜你喜欢
    • 2023-02-25
    • 2021-09-20
    • 2017-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-06
    • 1970-01-01
    • 2021-05-01
    相关资源
    最近更新 更多