2016-08-22 20 views
0

私は、入力としてJSONObjectを読んでいると私はnet.sf.json APIのgetString()方法を使用してキー「id」の値を取得していますが、私はnet.sf.JSONObjectはのjava.lang.String例外にキャストすることはできません取得しています

//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 
} 
+0

印刷 'id'値と、それはあなたが私がoptstringに( "ID"、null)の代わりにはgetString() –

+0

を印刷しますかを見ますJSONObject jsonObject.fromObject(someString);このコードの作業ですか? – Rekha

+0

を使用する必要があるわけ –

答えて

0

使用optString("id",null)isNull()確認後、ときにわからないJSON形式について同じこととになります:私は

INPUT: 
    { 
    "id" : null 
    } 

コード...それはブロックの場合には行っていない理由を知るために好奇心が強いですハンドル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{ 

} 

または

jsonObject.optJSONObjet(String arg1) 

read this

+0

を使用する場所 – Rekha

+0

このoptstringには、()に何をするかと、私はこの中にid値を印刷する場合、ブロック、それは「ヌル」を印刷した場合、あなたが私に言うことができるのisNull() –

関連する問題