gpt4 book ai didi

java - 在 JSON 中追加或删除数据

转载 作者:行者123 更新时间:2023-12-02 13:34:06 24 4
gpt4 key购买 nike

我想要得到的结果:

[
{"id":1,"name":"example1","description":"An example"},
{"id":2, "name":"example2","description":"Just another example"},
... ]

要将新数据添加到 JSON,我尝试了以下操作:

String jsonDataString = ALL MY JSON DATA HERE;
JSONObject mainObject = new JSONObject(jsonDataString);
JSONObject valuesObject = new JSONObject();
JSONArray list = new JSONArray();
valuesObject.put("id", "3");
valuesObject.put("name", "example3");
valuesObject.put("description", "Yet another example");
list.put(valuesObject);
mainObject.accumulate("", list);

但我没有得到正确的结果。

如何根据 ID 的值删除 JSON 数据?

谢谢。

最佳答案

  1. 要构建新的 JSONArray,您可以使用以下代码:

    try {

    JSONArray jsonArray = new JSONArray();

    // Object 1
    JSONObject jsonObject1 = new JSONObject();
    jsonObject1.put("id", 1);
    jsonObject1.put("name", "example1");
    jsonObject1.put("description", "An example");

    // Object 2
    JSONObject jsonObject2 = new JSONObject();
    jsonObject2.put("id", 2);
    jsonObject2.put("name", "example2");
    jsonObject2.put("description", "Just another example");

    // Add Object 1 & 2 JSONArray
    jsonArray.put(jsonObject1);
    jsonArray.put(jsonObject2);


    Log.d("JSON", "JSON: " + jsonArray.toString());

    } catch (final JSONException e) {
    Log.e("FAILED", "Json parsing error: " + e.getMessage());
    }

输出:

D/JSON: JSON: [{"id":1,"name":"example1","description":"An example"},{"id":2,"name":"example2","description":"Just another example"}]
  • 要将新的 JSONObject 添加到现有的 JSONArray 中,您可以使用以下代码:

    // Your Existing JSONArray
    // [{"id":1,"name":"example1","description":"An example"},
    // {"id":2, "name":"example2","description":"Just another example"}]

    String jsonDataString = "[{\"id\":1,\"name\":\"example1\",\"description\":\"An example\"},{\"id\":2,\"name\":\"example2\",\"description\":\"Just another example\"}]";

    try {

    JSONArray jsonArray = new JSONArray(jsonDataString);

    // Object 3
    JSONObject jsonObject3 = new JSONObject();
    jsonObject3.put("id", 3);
    jsonObject3.put("name", "example3");
    jsonObject3.put("description", "Third example");

    // Add Object 3 JSONArray
    jsonArray.put(jsonObject3);

    Log.d("JSON", "JSON: " + jsonArray.toString());

    } catch (final JSONException e) {
    Log.e("FAILED", "Json parsing error: " + e.getMessage());
    }
  • 输出:

    D/JSON: JSON: [{"id":1,"name":"example1","description":"An example"},{"id":2,"name":"example2","description":"Just another example"},{"id":3,"name":"example3","description":"Third example"}]
  • 要从 JSONArray 中删除 JSONObject,您可以使用以下代码:

    // Your Existing JSONArray
    // [{"id":1,"name":"example1","description":"An example"},
    // {"id":2,"name":"example2","description":"Just another example"},
    // {"id":3,"name":"example3","description":"Third example"}]

    String jsonDataString = "[{\"id\":1,\"name\":\"example1\",\"description\":\"An example\"},{\"id\":2,\"name\":\"example2\",\"description\":\"Just another example\"},{\"id\":3,\"name\":\"example3\",\"description\":\"Third example\"}]";

    try {

    JSONArray jsonArray = new JSONArray(jsonDataString);

    Log.d("JSON", "JSON before Remove: " + jsonArray.toString());

    // Remove Object id = 2
    int removeId = 2;

    for (int i = 0; i < jsonArray.length(); i++)
    {
    JSONObject object = jsonArray.getJSONObject(i);

    if (object.has("id") && !object.isNull("id")) {

    int id = object.getInt("id");

    if (id == removeId)
    {
    jsonArray.remove(i);
    break;
    }
    }
    }

    Log.d("JSON", "JSON After Remove: " + jsonArray.toString());

    } catch (final JSONException e) {
    Log.e("FAILED", "Json parsing error: " + e.getMessage());
    }
  • 输出:

    D/JSON: JSON Before Remove: [{"id":1,"name":"example1","description":"An example"},{"id":2,"name":"example2","description":"Just another example"},{"id":3,"name":"example3","description":"Third example"}]

    D/JSON: JSON After Remove: [{"id":1,"name":"example1","description":"An example"},{"id":3,"name":"example3","description":"Third example"}]

    希望这对您有帮助。

    关于java - 在 JSON 中追加或删除数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43103200/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com