Json数组在Java中使用
Json例
[
{
"name": "ceshi",
"age": 18,
"classmates": [
{
"name": "cece",
"age": 19
},
{
"name": "cecece",
"age": 17
}
]
}
]
// result是上面的json串
String result = ""
JSONArray jsonArray = null;
jsonArray = new JSONArray(result);
String name = (String) jsonArray.getJSONObject(0).get("name");// ceshi
Integer age = (Integer) jsonArray.getJSONObject(0).get("age");// 18
JSONArray classmatesJSONArray = (JSONArray) jsonArray.getJSONObject(0).get("classmates");
// classmatesJSONArray的长度是classmatesJSONArray.length()
String classmateName = (String) classmatesJSONArray.getJSONObject(0).get("name");// cece
String classmateName2 = (String) classmatesJSONArray.getJSONObject(1).get("name");// cecece
转载:https://blog.csdn.net/a978292982/article/details/101030240
查看评论