-3

Web サービス用に次の配列があります: {"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();}
4

2 に答える 2

1

提供した JSON を解析するには、次のようなものが必要です。

   JSONObject jsonObject = new JSONObject(jsonString);

   update = jsonObject.optBoolean("update");
   message = jsonObject.optString("msg");
   role = jsonObject.optString("role");
于 2015-07-15T16:33:44.710 に答える