日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

android判断是否json格式,android – 检查JSON中是否存在subObject

發布時間:2025/3/12 javascript 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android判断是否json格式,android – 检查JSON中是否存在subObject 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

您可以嘗試一下,如果嘗試失敗,則返回null.或者你可以將你的嘗試分解成小塊來監視失敗的地方.

/**

* This method will return the JSONObject q0, if it exists

* If it doesn't exist it will return NULL

*

*/

private JSONObject getQZero(JSONObject json)

{

try

{

return json.getJSONObject("queue").getJSONObject("building").getJSONObject("q0");

}

catch (JSONException e)

{

// This could be triggered either because there is no q0

// or because the JSON structure is different from what was expected.

return null;

}

}

如果你想打印每個級別的日志,你也可以一步一步走;

/**

* This method will show where your jsonparsing fails.

* It will throw a JSONOException if the json is way different from what

* was expected, and otherwise it will print a log of where the parsing

* failed.

*/

private JSONObject getQZero(JSONObject json) throws JSONException

{

// Stop if no queue

if (! myObject.has("queue")

{

Log.d(TAG, "no queue!");

return null;

}

JSONObject queue = myObject.getJSONObject("queue");

// Stop if no building

if (! queue.has("building")

{

Log.d(TAG, "no building!");

return null;

}

JSONObject building = queue.getJSONObject("building")

// Stop if no q0

if (! building.has("q0"))

{

Log.d(TAG, "no q0!");

return null;

}

JSONObject q0 = building.getJSONObject("q0");

// Q0 is returned here. If the method returned earlier, it returned NULL

// You could also do nested ifs, but the indentation gets crazy

return q0;

}

總結

以上是生活随笔為你收集整理的android判断是否json格式,android – 检查JSON中是否存在subObject的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。