facebook twitter hatena line email

Java/json

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
// json // {"bglayers":[{"kind":"default","layernums":[1,2]},{"kind":"day","layernums":[2,3]}]}
json = "{'bglayers':[{'kind':'renge','startdate':'2010-10-10 10:10','enddate':'2013-10-10 10:10','layernums':[3,4]},{'kind':'default','layernums':[1,2]}]}";
rootObject = new JSONObject(json);
JSONArray bglayers = rootObject.getJSONArray("bglayers");
for (int i = 0; i < bglayers.length(); i++) {
    JSONObject bglayer = bglayers.getJSONObject(i);
    String kind = bglayer.getString("kind");
    JSONArray layernums = bglayer.getJSONArray("layernums")
    int layernum = layernums.getInt(i)
}

キーが有るか判定

if (jsonObj.isNull("bglayers")) {
  // キーがない
} else {
  // キーがある
}

マニュアル

http://developer.android.com/reference/org/json/JSONArray.html