javascript
jsp java循环读取json_JAVA JSON遍历问题,求解(内附代码)
問題描述:package?testJSON;
import?net.sf.json.JSONArray;
import?net.sf.json.JSONObject;
public?class?TestJson2?{
public?static?void?main(String[]?args)?{
String?str?=?"{"data":[{"id":"1","name":"name1"},"
+?"{"id":"2","name":"Zname2.0","name":"Dname2.1","name":"Pname2.2","name":"Zname2.3"},{"id":"3","name":"name3"},]}";
//
JSONObject?jsonObject?=?JSONObject.fromObject(str);
JSONArray?result?=?jsonObject.getJSONArray("data");
//System.out.println(result);
for?(int?i?=?0;?i?
String?id?=?result.getJSONObject(i).getString("id");
if(id.equals("2")){
String?name?=?result.getJSONObject(i).getString("name");
System.out.println(name);//["Zname2.0","Dname2.1","Pname2.2","Zname2.3"]
//怎么取出["Zname2.0","Dname2.1","Pname2.2","Zname2.3"]中的"Dname2.1"????
}
}
}
}
問題解答:String[]?strs=name.split(",");
List?asList?=?Arrays.asList(strs);
System.out.println(asList.get(1));
先分割成數(shù)組,然后轉(zhuǎn)成集合,最后直接取下標
總結
以上是生活随笔為你收集整理的jsp java循环读取json_JAVA JSON遍历问题,求解(内附代码)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 9008刷机模式写入超时刷机帮_刷机时没
- 下一篇: tcpsyn发生在哪层_必看面试题之计算