【发布时间】:2015-07-15 16:26:26
【问题描述】:
我的网络服务有这个数组:{"update":true,"msg":"Logged in Bro","role":"admin"}
现在,我需要在 Android 中使用它来处理我的应用程序,而不是登录。所以我在这里需要一种将这些数据格式化为名称值对或单个变量的方法,以便我可以使用它们。
我目前正在使用它,但它会强制关闭我的应用程序:
try {
JSONObject jsonRootObject = new JSONObject(result);
//Get the instance of JSONArray that contains JSONObjects
JSONArray jsonArray = jsonRootObject.optJSONArray("");
//Iterate the jsonArray and print the info of JSONObjects
for(int i=0; i < jsonArray.length(); i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
update = jsonObject.optString(jsonObject.optString("update").toString());
message = jsonObject.optString("msg").toString();
role = jsonObject.optString(jsonObject.optString("role").toString());
}
} catch (JSONException e) {e.printStackTrace();}
【问题讨论】:
-
哪一行导致崩溃?
-
json 不包含数组(没有 [ ] )
-
所以在你提供的json中,没有数组,只有一个JSONObject包含3个值:boolean "update",和两个Strings:"msg"和"role"
标签: android arrays json rest name-value