我有以下 json 字符串,并想使用 Java 访问 alsoKnownAs 列表。我可以使用 getString 作为名称,我已经为 alsoKnownAs 尝试过 getJSONArray 但它并没有完全锻炼
{\"name\":\"moses\",\"alsoKnownAs\":[\"njai\", \"njenga\",\"musa\"]}
我可以访问下面的名称,但我不能使用等效的 getString 方法返回字符串列表或等效的 getJSONArray 以获取字符串列表
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");
//I was trying this to get the list but figure out I was using a list of json objects, so not how to get the list of stings
JSONArray arrayKnownAs = names.getJSONArray("alsoKnownAs");
List<String> alsoKnownAs= new ArrayList<>();
for (int i = 0, l = arrayKnownAs.length(); i < l; i++) {
String origin;
origin = arrayKnownAs[i];
alsoKnownAs.add(origin);
}
Person thisPerson = new Person(
//I instantiate person object here
);
return thisPerson;
} catch (org.json.JSONException e) {
// error
}
return null;
}
弑天下
相关分类