怎麼判斷下列其中一項是空值的話把該項填入null這個字串呢,因為如果是空值的話輸出就不會顯示出來甚麼,我想讓他輸出null......
JSONObject data;
for (int i = 0; i < list.length(); i++) {
data = list.optJSONObject(i);
mSearchList.add(data.optString("o") + " " + data.optString("u")
+ " " + data.optString("a")+ " " + data.optString("b")
+ " " + data.optString("c")+ " " + data.optString("d")
+ " " + data.optString("e")+ " " + data.optString("f")
+ " " + data.optString("g")+ " " + data.optString("h")
+ " " + data.optString("i")+ " " + data.optString("j")
+ " " + data.optString("k")+ " " + data.optString("l")
+ " " + data.optString("n"));}
參考看看資料來源
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);
}
我想問是我真沒救了嗎,我好像怎麼看都沒辦法把我的例子改成這樣,因為是一串多個.optString的輸出,並且是return方式,嘗試過後失敗,請原諒我真的那麼問了....
改用這個試試看
JSONObject data;
for (int i = 0; i < list.length(); i++) {
data = list.optJSONObject(i);
mSearchList.add(data.optString("o","null") + " " + data.optString("u","null")
+ " " + data.optString("a","null")+ " " + data.optString("b","null")
+ " " + data.optString("c","null")+ " " + data.optString("d","null")
+ " " + data.optString("e","null")+ " " + data.optString("f","null")
+ " " + data.optString("g","null")+ " " + data.optString("h","null")
+ " " + data.optString("i","null")+ " " + data.optString("j","null")
+ " " + data.optString("k","null")+ " " + data.optString("l","null")
+ " " + data.optString("n","null"));}