我得net.sf.JSONObject无法强制转换为java.lang.String Exception

时间:2016-08-22 12:03:41

标签: java json if-statement jsonobject getstring

我正在阅读JSONObject作为输入,我正在使用id API的getString()方法检索密钥“net.sf.json”的值但我很好奇知道为什么它不会进入if块..

INPUT:
    {
    "id" : null
    }

代码:

//reading the jsonObject from String
JSONObject jsonObject.fromObject(someString);
String id = jsonObject.getString("id");
if( id == null)
{
        //the control is not going in this if condition
}

1 个答案:

答案 0 :(得分:0)

license.Validate() .AssertThat(lic => lic.ProductFeatures.Get("HardwareId") == "133456", new GeneralValidationFailure() { Message="Invalid Hardware.", HowToResolve="Contact administrator"}); 检查后使用optString("id",null),当您不确定JSON格式是否相同并处理isNull()时。{/ p>

NPE

JSONObject jO=jsonObject.fromObject(someString);
String id = jO.optString("id",null);
if(jO.isNull("id"))
{
    //the control is not going in this if condition
}
else{

}

read this