简体   繁体   中英

Java - update object in array of objects

I would like to update an object in array of objects.

For example, in this object:

Form form = 
{
  "id": "c84d93a3-c0fd-4f53-8b72-76e8eXXXXXX",
  "book": {
    "register": [
      {
        "header": "Header 1",
        "questions": [
          {
            "id": "87b8c60c-9c9e-41d1-a52a-588400XXXXXX",
            "label": "Question 1-A",
            "answers": [
              {
                "id": 1,
                "defaultValue": "Hello world",
                "layout": "SIMPLE",
                "textAllowedValue": "TEXT",
                "textTransformation": "UPPERCASE",
                "maxLength": 120,
                "userValue": "HELLO WORLD"
              }
            ]
          }
        ]
      },
      {
        "header": "Header 2",
        "questions": [
          {
            "id": "87b8c60c-9c9e-41d1-a52a-588400XXXXXX",
            "label": "Question 2-A",
            "answers": [
              {
                "id": 1,
                "defaultValue": "Answer 1",
                "layout": "SIMPLE",
                "textAllowedValue": "TEXT",
                "textTransformation": "UPPERCASE",
                "maxLength": 120,
                "userValue": "ANSWER 1"
              },
              {
                "id": 2,
                "defaultValue": "Answer 2",
                "layout": "SIMPLE",
                "textAllowedValue": "TEXT",
                "textTransformation": "UPPERCASE",
                "maxLength": 120,
                "userValue": "ANSWER 1"
              },
              {
                "id": 3,
                "defaultValue": "Answer 3",
                "layout": "SIMPLE",
                "textAllowedValue": "TEXT",
                "textTransformation": "UPPERCASE",
                "maxLength": 120,
                "userValue": "ANSWER 3"
              }
            ]
          },
          {
            "id": "87b8c60c-9c9e-41d1-a52a-588400XXXXXX",
            "label": "Question 2-B",
            "answers": [
              {
                "id": 1,
                "defaultValue": "Answer 1",
                "layout": "SIMPLE",
                "textAllowedValue": "TEXT",
                "textTransformation": "UPPERCASE",
                "maxLength": 120,
                "userValue": "ANSWER 1"
              },
              {
                "id": 2,
                "defaultValue": "Answer 2",
                "layout": "SIMPLE",
                "textAllowedValue": "TEXT",
                "textTransformation": "UPPERCASE",
                "maxLength": 120,
                "userValue": "ANSWER 1"
              },
              {
                "id": 3,
                "defaultValue": "Answer 3",
                "layout": "SIMPLE",
                "textAllowedValue": "TEXT",
                "textTransformation": "UPPERCASE",
                "maxLength": 120,
                "userValue": "ANSWER 3"
              }
            ]
          }
        ]
      }
    ]
  }
}

I want to update this 'Answer' object:

{
                "id": 3,
                "defaultValue": "Answer 3",
                "layout": "SIMPLE",
                "textAllowedValue": "TEXT",
                "textTransformation": "UPPERCASE",
                "maxLength": 120,
                "userValue": "ANSWER 3"
              }

by this object:

newObject = {
                "id": 3,
                "defaultValue": "Answer 4",
                "layout": "MULTIPLE",
                "textAllowedValue": "TEXT",
                "textTransformation": "UPPERCASE",
                "maxLength": 150,
                "userValue": "ANSWER 4"
              }

The ideal would be to have a library (such as ObjectMapper()) that allows you to do the following line:

formUpdated = new ObjectMapper().updateWithObject(form, newObject) 

Is it possible to map by several keys so that the mapper identifies the object to update and does the update at the same time?

You need to first parse the JSON into a Form POJO, then you can do something like

Answer[] answers = 
  form.getBook().getRegisters()[1].getQuestions()[0].getAnswers();
answers[2] = newAnswer;

// convert back
String out = mapper.writeValueAsString(form);

If you want to do individual fields, then answers[2].setFoo(newBar)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM