instanceof JSONObject没有检测到JSONObject

时间:2016-01-15 11:48:47

标签: android json

我发现了variable instanceof JSONObject的奇怪行为。我有以下代码:

Object value;
try {
    value = customer.get(key);
    Log.v("type", String.valueOf(value.getClass())); // Returns JSONObject
    if (value instanceof JSONObject) {
        // Handle JSONObject
    } else {
        String text = (String)value;  // HERE COMES THE ERROR
        // Handle String
    }

当日志返回JSONObject时,它最终会出现异常:

01-15 12:24:46.206 4111-4111/cz.jacon.goldympos V/CUSTOMER type: class org.json.JSONObject$1
01-15 12:24:46.207 4111-4111/cz.jacon.goldympos D/AndroidRuntime: Shutting down VM
01-15 12:24:46.279 4111-4111/cz.jacon.goldympos E/AndroidRuntime: FATAL EXCEPTION: main
                   Process: cz.jacon.goldympos, PID: 4111
                   java.lang.RuntimeException: Unable to start activity ComponentInfo{cz.jacon.goldympos/cz.jacon.goldympos.activities.CustomerDetailActivity}: java.lang.ClassCastException: org.json.JSONObject$1 cannot be cast to java.lang.String
                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
                            at android.app.ActivityThread.access$800(ActivityThread.java:151)
                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
                            at android.os.Handler.dispatchMessage(Handler.java:102)
                            at android.os.Looper.loop(Looper.java:135)
                            at android.app.ActivityThread.main(ActivityThread.java:5254)
                            at java.lang.reflect.Method.invoke(Native Method)
                            at java.lang.reflect.Method.invoke(Method.java:372)
                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
                            Caused by: java.lang.ClassCastException: org.json.JSONObject$1 cannot be cast to java.lang.String
                            at cz.jacon.goldympos.activities.CustomerDetailActivity.getText(CustomerDetailActivity.java:260)
                            at cz.jacon.goldympos.activities.CustomerDetailActivity.createLayout(CustomerDetailActivity.java:222)
                            at cz.jacon.goldympos.activities.CustomerDetailActivity.onCreate(CustomerDetailActivity.java:92)
                            at android.app.Activity.performCreate(Activity.java:5990)
                            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                        ...........

我错过了什么吗?它看起来很简单......

编辑:json

我使用的json示例。首先是JSONObject,第二个是Integer(可以转换为String)

{
"viewers_edit_time":
    {
     "date":"2016-01-14 22:53:56.000000",
     "timezone_type":3,
     "timezone":"Asia/Kolkata"
    },
"viewers_edit_users_id":1
}

That's full json,我使用的完全相同。再说一次:

Log.v("type", String.valueOf(value.getClass())); // Returns JSONObject not JSONObject$1

解决

问题解决了。从我发布的完整JSON中可以看出:

"viewers_postcode": null

我没有注意到null不是引号......在我以为我是" null"这是String。我被错误的错误信息弄错了,其中显示JSONObject$1 cannot be casted to String。但是JSONObject $1为NULL(归功于@Natix)

对于具有相同错误消息的其他人可能会有所帮助。

1 个答案:

答案 0 :(得分:3)

您需要特别注意日志消息。关键是org.json.JSONObject$1 与班级org.json.JSONObject相同。

$1后缀表示它是JSONObject类的匿名内部类。如果你查看the source,你会发现这段代码:

public static final Object NULL = new Object() {
    @Override public boolean equals(Object o) {
        return o == this || o == null; // API specifies this broken equals implementation
    }
    @Override public String toString() {
        return "null";
    }
};

这意味着您应首先检查您的值是否等于此NULL常量:

Object value = customer.get(key);
if (value == JSONObject.NULL {
    // Handle NULL
} else if (value instanceof JSONObject) {
    // Handle JSONObject
} else {
    // Handle String
}