JSON Volley PUT 请求覆盖一切

我正在尝试使用 Volley for Android 更新远程 JSON 值。问题是下面的代码完全覆盖了整个 JSON 对象。


文件位于此处:https ://api.myjson.com/bins/kubxi


原始 JSON 文件如下所示:


{

  "females": [

    {

      "id": 1,

      "name": "Name One",

      "actions": [

        {

          "action_1": 1,

          "action_2": 2,

          "action_3": 3

        }

      ]

    },

    {

      "id": 2,

      "name": "Name Two",

      "actions": [

        {

          "action_1": 4,

          "action_2": 5,

          "action_3": 6

        }

      ]

    }

  ]

}

Java 代码


private void sendRequest() {

        RequestQueue queue = Volley.newRequestQueue(this);

        final JSONObject jsonObject = new JSONObject();

        String url ="https://api.myjson.com/bins/kubxi"; // Remote JSON file


        try {

            jsonObject.put("action_1", 123);

            jsonObject.put("action_2", 456);

            jsonObject.put("action_3", 789);

        } catch (JSONException e) {

            Log.d("Exception", e.toString());

        }


        JsonObjectRequest putRequest = new JsonObjectRequest(Request.Method.PUT, url, jsonObject,

                new Response.Listener<JSONObject>()

                {

                    @Override

                    public void onResponse(JSONObject response) {

                        Log.d("Response", response.toString());

                    }

                },

                new Response.ErrorListener()

                {

                    @Override

                    public void onErrorResponse(VolleyError error) {

                        Log.d("Error.Response", error.toString());

                    }

                }

        )


使用此代码后,JSON 文件如下所示:


{

    "action_1": 123,

    "action_2": 456,

    "action_3": 789

}

我期望代码仅将 action_1、action_2 和 action_3 上的值从 1、2、3 更新为 123、456、789。

建议将不胜感激!


米琪卡哇伊
浏览 115回答 1
1回答

肥皂起泡泡

要更新 json 文件中的特定值,您可以这样做:首先将您的originaljson 放入 String :&nbsp; String jsonString ="{&nbsp; "females": [&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "id": 1,&nbsp; &nbsp; &nbsp; "name": "Name One",&nbsp; &nbsp; &nbsp; "actions": [&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "action_1": 1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "action_2": 2,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "action_3": 3&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; }&nbsp; ]}";接下来,将此字符串传入JsonObject:&nbsp; &nbsp;JSONObject jObject&nbsp; = new JSONObject(jsonString);//passing string to jsonobject&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;JSONArray jsonArray = jObject.getJSONArray("females");&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < jsonArray.length(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;JSONObject object = jsonArray.getJSONObject(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;JSONArray jsonObject= object.getJSONArray("actions"); //getting action&nbsp;array&nbsp; &nbsp; &nbsp; &nbsp;for (int j = 0; j < jsonObject.length(); j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;JSONObject object1 = jsonObject.getJSONObject(j);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;object1.put("action_1", 123); //here you are putting value to action_1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; object1.put("action_2", 456);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; object1.put("action_3", 789);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;然后将其发送jsonObject到您的服务器。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java