我的json结构就像,
{
"members": [
{
"available": true,
"firstName": "Jackie",
"lastName": "Kane",
"religion": "christianity"
},
{
"available": true,
"firstName": "Micke",
"lastName": "Muhru",
"religion": "Buddhism"
}
]
}
我使用以下代码从文件中读取
InputStream inputStream = getResources().openRawResource(R.raw.quote);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int ctr;
try {
ctr = inputStream.read();
while (ctr != -1) {
byteArrayOutputStream.write(ctr);
ctr = inputStream.read();
}
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
JSONObject jObject = null;
try {
jObject = new JSONObject(
byteArrayOutputStream.toString());
} catch (JSONException e) {
e.printStackTrace();
}
Gson g = new Gson();
MemberList responseData = g.fromJson(jObject.toString(), MemberList .class);
现在我需要在成员Object中添加另一个属性memberId
。
新的Json文件看起来应该是,
{
"members": [
{
"memberId": 1,
"available": true,
"firstName": "Jackie",
"lastName": "Kane",
"religion": "christianity"
},
{
"memberId": 2,
"available": true,
"firstName": "Micke",
"lastName": "Muhru",
"religion": "Buddhism"
}
]
}
如何将其写入Raw文件夹中的文件?
答案 0 :(得分:0)
将另一个变量 memberId 添加到您的类 MemberList 中,在解析JSON结果后,使用您的值填充memberId。
public class Member {
@SerializedName("memberId")
private int memberId;
@SerializedName("available")
private boolean available;
@SerializedName("firstName")
private String firstName;
@SerializedName("lastName")
private String lastName;
@SerializedName("religion")
private String religion;
public void setMemberId(int memberId) {
this.memberId = memberId;
}
}