大神帮忙解答一下这个json数据怎么在java里用json lib解析存放到List里
[{"city":"","citys":[{"city":"北京","id":2,"level":2,"province":"北京","seq":0,"x":116.40...
[
{
"city": "",
"citys": [
{
"city": "北京",
"id": 2,
"level": 2,
"province": "北京",
"seq": 0,
"x": 116.405,
"y": 39.905
}
],
"id": 1,
"level": 1,
"province": "北京",
"region": "华北地区",
"seq": 0,
"x": 116.405,
"y": 39.905
},
{
"city": "",
"citys": [
{
"city": "上海",
"id": 4,
"level": 2,
"province": "上海",
"seq": 0,
"x": 121.473,
"y": 31.2317
}
],
"id": 3,
"level": 1,
"province": "上海",
"region": "华东地区",
"seq": 0,
"x": 121.473,
"y": 31.2317
}
] 展开
{
"city": "",
"citys": [
{
"city": "北京",
"id": 2,
"level": 2,
"province": "北京",
"seq": 0,
"x": 116.405,
"y": 39.905
}
],
"id": 1,
"level": 1,
"province": "北京",
"region": "华北地区",
"seq": 0,
"x": 116.405,
"y": 39.905
},
{
"city": "",
"citys": [
{
"city": "上海",
"id": 4,
"level": 2,
"province": "上海",
"seq": 0,
"x": 121.473,
"y": 31.2317
}
],
"id": 3,
"level": 1,
"province": "上海",
"region": "华东地区",
"seq": 0,
"x": 121.473,
"y": 31.2317
}
] 展开
1个回答
2015-11-28
展开全部
import java.util.List;
import net.sf.json.JSONArray;
public class Spider
{
public static void main(String[] args)
{
String jsonstr = "[ { \"city\": \"\", \"citys\": [ { \"city\": \"北京\", \"id\": 2, \"level\": 2, \"province\": \"北京\", \"seq\": 0, \"x\": 116.405, \"y\": 39.905 } ], \"id\": 1, \"level\": 1, \"province\": \"北京\", \"region\": \"华北地区\", \"seq\": 0, \"x\": 116.405, \"y\": 39.905 }, { \"city\": \"\", \"citys\": [ { \"city\": \"上海\", \"id\": 4, \"level\": 2, \"province\": \"上海\", \"seq\": 0, \"x\": 121.473, \"y\": 31.2317 } ], \"id\": 3, \"level\": 1, \"province\": \"上海\", \"region\": \"华东地区\", \"seq\": 0, \"x\": 121.473, \"y\": 31.2317 }]";
JSONArray array = JSONArray.fromString(jsonstr);
List list = JSONArray.toList(array);
System.out.println(list);
}
}
追问
你的json_lib是什么版本的?我的没有fromString这个方法啊
追答
net.sf版本的json库
json-lib-0.9.jar
commons-logging.jar
commons-lang-2.4.jar
commons-beanutils-1.8.3.jar
ezmorph-1.0.jar
2. fastjson库
fastjson-1.1.36.jar
3.谷歌gson库
gson-1.7.1.jar
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询