2 回答
TA贡献1818条经验 获得超8个赞
如果其他人被困在这里,结果证明我在正确的轨道上,我可以使用 getJSONArray 访问列表,但是在为每个成员迭代时,我使用 getString
public static Person parsePersonJson(String json) {
JSONObject currentPerson;
String name;
try {
currentPerson = new JSONObject(json);
// so I can access the name like
name = currentPerson.getString("name");
List<String> alsoKnownAs= new ArrayList<>();
//use getJSONArray to get the list
JSONArray arrayKnownAs = currentPerson.getJSONArray("alsoKnownAs");
for (int i = 0, l = arrayKnownAs.length(); i < l; i++) {
//This is where I was getting it wrong, i needed to use getString to access list items
alsoKnownAs.add(arrayKnownAs.getString(i));
}
Person thisPerson = new Person(
//I instantiate person object here
);
return thisPerson;
} catch (org.json.JSONException e) {
// error
}
return null;
}
添加回答
举报