如何使用 volley post 方法更新嵌套 json 对象的值?

How to update value of nested json object using volley post methd?

我正在做一个项目,用户将在其中输入他的数据,我们需要使用 API 存储数据。我必须更新嵌套 json 对象中的值。

我在 volley 中使用 POST 方法来更新数据。我的问题是我无法更新嵌套的 JSON 对象。

我也在使用 GSON,所以我有模型 class 作为结果、地址和联系方式。

更新前

{
    "id": 58,
    "address": null,
    "contact": null
}

更新后

  {
    "id": 58,
    "address": {
        "id":50,
        "first_line": "first_line",
        "locality": null,
        "state": "state",
        "country": "country"
    },
    "contact": {
       "primary_number": "primary",
       "secondary_number": "secondary"
    }
}

谢谢

您可以尝试以下方法

try {
            JSONObject obj=new JSONObject();

            obj.put("id", 58);

            JSONObject contact=new JSONObject();
            contact.put("id", "smome value");
            contact.put("first_line", "smome value");
            contact.put("locality", "smome value");

            JSONObject address=new JSONObject();
            address.put("primary_number", "primary");
            address.put("secondary_number", "secondary");

            obj.put("address", address);
            obj.put("contact", contact);
        }catch (Exception e) {
            e.printStackTrace();
        }

现在输出如下所示

   {
  "id": 58,
  "address": {
    "primary_number": "primary",
    "secondary_number": "secondary"
  },
  "contact": {
    "first_line": "smome value",
    "locality": "smome value",
    "id": "smome value"
  }
}