第一種:php
[
{
"0": "1",
"1": "一",
"id": "1",
"nam": "一"
},
{
"0": "2",
"1": "二",
"id": "2",
"nam": "新二"
},
{
"0": "3",
"1": "三",
"id": "3",
"nam": "三"
},
]json
解析方法:第一種直接是數組因此直接獲取到jsonArray直接拿來解析用循環獲取對象而後獲取裏面的值數組
List name = new ArrayList<String>();
try
{
JSONArray jsonArray = new JSONArray(result);
for (int j = 0; j < jsonArray.length(); j++)
{
JSONObject jsonObject= jsonArray.getJSONObject(j);
String title= jsonObject.getString("nam");
name.add(title);
}
}
catch (Exception e)
{
e.printStackTrace();
}
第二種:url
比第一種多了一箇中括號 對象
List iconName= new ArrayList<String>();
try{
JSONObject jsonObject = new JSONObject(result);//獲取json對象
JSONObject type=jsonObject.getJSONObject("type");//type裏面的數據
JSONArray jsonArray=jsonObject.getJSONArray("conts");//將有中括號中的json數據放入JsonArray裏
Log.e("length",jsonArray.length()+"");
for (int j = 0; j < jsonArray.length(); j++) //循環獲取裏面的數據
{
JSONObject json= jsonArray.getJSONObject(j);
System.out.print(jsonObject.toString());
String title= json.getString("nam");
// Log.e("title",title);
iconName.add(title); //將獲取到的數據放入集合
}
}
catch (Exception e)
{
e.printStackTrace();
}
{
"type": {
"0": "1",
"1": "本院介紹",
"2": "1",
"3": "100.php?id=1&fun=load",
"id": "1",
"nam": "本院介紹",
"weight": "1",
"url": "100.php?id=1&fun=load"
},
"conts": [
{
"0": "1",
"1": "一",
"id": "1",
"nam": "一"
},
{
"0": "3",
"1": "三",
"id": "3",
"nam": "三"
},
{
"0": "2",
"1": "二",
"id": "2",
"nam": "二"
}
]
}get