0

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

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

2 回答 2

1
 you did not declared jsonObject 
于 2013-02-28T14:19:27.237 回答
0

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

于 2013-03-01T09:56:59.447 回答