1

For example, I'm getting the following JSON response:

{
  "metadata": {
    "provider": "ABC"
  },
  "results": [
    {
      "id": "ace",
      "language": "en",
      "lexicalEntries": [
        {
          "lexicalCategory": "Noun"
        },
        { 
          "lexicalCategory": "Adjective"
        },
        {
          "lexicalCategory": "Verb"
        }
      ],
      "type": "headword",
      "word": "ace"
    }
  ]
}

I'm using Retrofit2.0 and gson to parse it and map to POJO classes in the following manner:

class QueryResponse {
    public Metadata metadata;
    public List<Result> results = null;
}
class Result {
    public String id;
    public String language;
    public List<LexicalEntry> lexicalEntries = null;
    public String type;
    public String word;
}

class LexicalEntry {
    public String lexicalCategory;
}

The onResponse() looks like this:

@Override
public void onResponse(Call<QueryResponse> call, Response<QueryResponse> response) {
     if (response.code() == 200) {
         Result result = response.body().results.get(0);
         lexicalEntries = result.lexicalEntries;
     }
}

Now I have another method in which I want to create a new LexicalEntry object of my own (namely, revisedLexicalEntry) and copy the value from what was retrieved from JSON, and then modify it in my own way for reusing further.

private void createSubWords(List<LexicalEntry> lexicalEntries) {
    LexicalEntry revisedLexicalEntry;
    for (int x = 0; x < lexicalEntries.size(); x++) {
        revisedLexicalEntry = lexicalEntries.get(x);
        revisedLexicalEntry.lexicalCategory = "Something...";
    }
}

What happens is that when later on, i try to get the lexicalCategory of original JSON (lexicalEntries.get(x).lexicalCategory), it is also changed to "Something..." instead of what was its original value.

How can I achieve my objective of retaining the value of originals but copying and modifying it for further use? I just hope I made my question clear enough.

Thanking in advance!

P.S., the actual JSON I'm working with is far more complicated but I've simplified it here for better understanding and quicker suggestions.

Shahood ul Hassan
  • 745
  • 2
  • 9
  • 19
  • If you are looking to modify the json response and forward again the you can see my answer in https://stackoverflow.com/questions/35773028/how-to-change-body-in-okhttp-response/35773177#35773177 – Rohit5k2 May 29 '18 at 08:48
  • Well, I want to keep the original response and created POJO class objects intact with their original values. However, I want to take data from there and then modify that for further use. When I attempt to do that, the original data also changes. Your answer in the given link probably deals with changing the response. – Shahood ul Hassan May 29 '18 at 09:17

1 Answers1

0

How can I achieve my objective of retaining the value of originals but copying and modifying it for further use?

Make a new LexicalEntry object with your new value, rather than changing an existing object as you are doing now.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Perhaps, the above example is overly simplified. In actual, there are more fields in a LexicalEntry object than just a lexicalCategory. I want to retain all the other fields and change the value of just lexicalCategory. Thats why I want to first make a copy of the original and then modify it, instead of making a new LexicalEntry object. – Shahood ul Hassan May 29 '18 at 10:26
  • 1
    @ShahoodulHassan: "I want to retain all the other fields and change the value of just lexicalCategory" -- make a new `LexicalEntry` object with your new value for that field and assign it the old values for the other fields. "I want to first make a copy of the original and then modify it, instead of making a new LexicalEntry object" -- making a copy *is* making a new object. If you start with 1 object, and when you are done you have 2 objects, you have made a new object. The way to do that in Java is via the constructor, such as [a copy constructor](https://stackoverflow.com/q/5749218/115145). – CommonsWare May 29 '18 at 10:30
  • The copy constructor solution worked like a charm. Thanks! – Shahood ul Hassan May 29 '18 at 20:53
  • I also found another solution which is to clone the old object by implementing `Cloneable` interface, overriding clone() method and then using it on the old object. It also serves the purpose but which one is recommended: copy constructor or clone ? – Shahood ul Hassan May 29 '18 at 20:56
  • 1
    @ShahoodulHassan: A copy constructor [is the better choice](https://stackoverflow.com/a/2427946/115145) of those two. – CommonsWare May 29 '18 at 21:44
  • Thank u for all the help! – Shahood ul Hassan May 29 '18 at 23:49