如何复制和修改由 Retrofit2.0 的 JSON 响应创建的 POJO class 对象
How to copy and modify the POJO class objects created by JSON response from Retrofit2.0
例如,我收到以下 JSON 响应:
{
"metadata": {
"provider": "ABC"
},
"results": [
{
"id": "ace",
"language": "en",
"lexicalEntries": [
{
"lexicalCategory": "Noun"
},
{
"lexicalCategory": "Adjective"
},
{
"lexicalCategory": "Verb"
}
],
"type": "headword",
"word": "ace"
}
]
}
我正在使用 Retrofit2.0 和 gson 来解析它并按以下方式映射到 POJO 类:
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;
}
onResponse() 看起来像这样:
@Override
public void onResponse(Call<QueryResponse> call, Response<QueryResponse> response) {
if (response.code() == 200) {
Result result = response.body().results.get(0);
lexicalEntries = result.lexicalEntries;
}
}
现在我有另一种方法,我想在其中创建一个新的我自己的 LexicalEntry 对象(即 revisedLexicalEntry)并复制从 JSON 检索到的值,然后在我自己的中修改它进一步重用的方法。
private void createSubWords(List<LexicalEntry> lexicalEntries) {
LexicalEntry revisedLexicalEntry;
for (int x = 0; x < lexicalEntries.size(); x++) {
revisedLexicalEntry = lexicalEntries.get(x);
revisedLexicalEntry.lexicalCategory = "Something...";
}
}
后来发生的事情是,我尝试获取原始 JSON 的 lexicalCategory (lexicalEntries.get(x).lexicalCategory),它也改为 "Something..."它的原始价值是多少。
如何实现 objective 保留原件的价值但复制和修改它以供进一步使用?我只是希望我把我的问题说清楚了。
提前致谢!
P.S。我正在使用的实际 JSON 要复杂得多,但我在这里对其进行了简化,以便更好地理解和更快地提出建议。
How can I achieve my objective of retaining the value of originals but copying and modifying it for further use?
使用您的新值创建一个新的 LexicalEntry
对象,而不是像现在这样更改现有对象。
例如,我收到以下 JSON 响应:
{
"metadata": {
"provider": "ABC"
},
"results": [
{
"id": "ace",
"language": "en",
"lexicalEntries": [
{
"lexicalCategory": "Noun"
},
{
"lexicalCategory": "Adjective"
},
{
"lexicalCategory": "Verb"
}
],
"type": "headword",
"word": "ace"
}
]
}
我正在使用 Retrofit2.0 和 gson 来解析它并按以下方式映射到 POJO 类:
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;
}
onResponse() 看起来像这样:
@Override
public void onResponse(Call<QueryResponse> call, Response<QueryResponse> response) {
if (response.code() == 200) {
Result result = response.body().results.get(0);
lexicalEntries = result.lexicalEntries;
}
}
现在我有另一种方法,我想在其中创建一个新的我自己的 LexicalEntry 对象(即 revisedLexicalEntry)并复制从 JSON 检索到的值,然后在我自己的中修改它进一步重用的方法。
private void createSubWords(List<LexicalEntry> lexicalEntries) {
LexicalEntry revisedLexicalEntry;
for (int x = 0; x < lexicalEntries.size(); x++) {
revisedLexicalEntry = lexicalEntries.get(x);
revisedLexicalEntry.lexicalCategory = "Something...";
}
}
后来发生的事情是,我尝试获取原始 JSON 的 lexicalCategory (lexicalEntries.get(x).lexicalCategory),它也改为 "Something..."它的原始价值是多少。
如何实现 objective 保留原件的价值但复制和修改它以供进一步使用?我只是希望我把我的问题说清楚了。
提前致谢!
P.S。我正在使用的实际 JSON 要复杂得多,但我在这里对其进行了简化,以便更好地理解和更快地提出建议。
How can I achieve my objective of retaining the value of originals but copying and modifying it for further use?
使用您的新值创建一个新的 LexicalEntry
对象,而不是像现在这样更改现有对象。