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.