JSON Volley PUT 请求覆盖一切

JSON Volley PUT request is overriding everything

我正在尝试使用 Android 的 Volley 更新远程 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());
                    }
                }
        )
        {
            @Override
            public Map<String, String> getHeaders()
            {
                Map<String, String> headers = new HashMap<>();
                headers.put("Accept", "application/json");
                headers.put("Content-Type", "application/json");
                return headers;
            }

            @Override
            public byte[] getBody() {

                try {
                    Log.i("JSON", jsonObject.toString());
                    return jsonObject.toString().getBytes("UTF-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                return null;
            }
        };
        queue.add(putRequest);
}

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

{
    "action_1": 123,
    "action_2": 456,
    "action_3": 789
}

我原以为代码只会将 action_1、action_2 和 action_3 上的值从 1、2、3 更新为 123、456、789。

我希望 JSON 文件在 运行 代码之后看起来像这样:

{
  "females": [
    {
      "id": 1,
      "name": "Name One",
      "actions": [
        {
          "action_1": 123,
          "action_2": 456,
          "action_3": 789
        }
      ]
    },
    {
      "id": 2,
      "name": "Name Two",
      "actions": [
        {
          "action_1": 123,
          "action_2": 456,
          "action_3": 789
        }
      ]
    }
  ]
}

我们将不胜感激!

要更新 json 文件中的特定值,您可以这样做:

首先将你的 original json 放入字符串 :

  String jsonString ="{
  "females": [
    {
      "id": 1,
      "name": "Name One",
      "actions": [
        {
          "action_1": 1,
          "action_2": 2,
          "action_3": 3
        }
      ]
    }
  ]
}";

接下来,将此字符串传递给 JsonObject:

   JSONObject jObject  = new JSONObject(jsonString);//passing string to jsonobject
         JSONArray jsonArray = jObject.getJSONArray("females");
        for (int i = 0; i < jsonArray.length(); i++) {
         JSONObject object = jsonArray.getJSONObject(i);
         JSONArray jsonObject= object.getJSONArray("actions"); //getting action 
array
       for (int j = 0; j < jsonObject.length(); j++) {
         JSONObject object1 = jsonObject.getJSONObject(j);

           object1.put("action_1", 123); //here you are putting value to action_1
                    object1.put("action_2", 456);
                    object1.put("action_3", 789);
        }
        } 

然后将此 jsonObject 发送到您的服务器。