我的网络服务有这个数组: {"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();}