2013-02-28 115 views
0

我有以下的JSON和我想要解析阵列(汽车),解析JSON阵列错误“的JSONObject解决不了”

[ 
{ 
"name": "John", 
"city": "Berlin", 
"cars": [ 
"audi", 
"bmw" 
], 

当我用下面的代码试过我有错误

JSONParser parser = new JSONParser(); 

    JSONArray a = (JSONArray) parser.parse(new FileReader(
      "C:\\General\\Json\\json.txt")); 

    for (Object o : a) { 
     JSONObject person = (JSONObject) o; 

     String name = (String) person.get("name"); 
     System.out.println(name); 

     String city = (String) person.get("city"); 
     System.out.println(city); 

     String job = (String) person.get("job"); 
     System.out.println(job); 

    } 

这里是错误“jsonObject无法解析” 我应该如何克服它?

JSONArray cars = (JSONArray) jsonObject.get("cars"); 

回答

1
you did not declared jsonObject 
0

JSONArray汽车=(JSONArray),其中person.get( “汽车”);试试这个,而不是JSONArray汽车=(JSONArray)jsonObject.get(“cars”);此PSR也正确