JSON Volley PUT请求将覆盖所有内容

时间:2019-03-05 11:06:55

标签: java android json android-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());
                    }
                }
        )
        {
            @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
        }
      ]
    }
  ]
}

建议将不胜感激!

1 个答案:

答案 0 :(得分:1)

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

首先将您的original json放入String:

  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发送到您的服务器。