解析Json数组错误“jsonObject无法解析”

时间:2013-02-28 14:16:36

标签: java json parsing

我有以下Json,我想解析数组(汽车),

[
{
"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");

2 个答案:

答案 0 :(得分:1)

 you did not declared jsonObject 

答案 1 :(得分:0)

JSONArray cars =(JSONArray)person.get(“cars”);试试这个而不是JSONArray cars =(JSONArray)jsonObject.get(“cars”);这个PSR也正确