【发布时间】:2020-04-02 16:00:44
【问题描述】:
我有一个带有对象的 json 文件
{
"id": 387,
"name": "flatFive",
"coordinates": {
"x": 9.6,
"y": 2.2
},
"creationDate": {
"year": 2020,
"monthValue": 4,
"month": "APRIL",
"dayOfMonth": 1,
"dayOfYear": 92,
"dayOfWeek": "WEDNESDAY",
"hour": 20,
"minute": 40,
"second": 47,
"nano": 662000000,
"chronology": {
"id": "ISO",
"calendarType": "iso8601"
}
},
"area": 332.3,
"numberOfRooms": 3,
"furnish": "bad",
"view": "NORMAL",
"transport": "NONE",
"house": {
"name": "Cottage",
"year": 3,
"numberOfLifts": 6
}
那么,如何从“坐标”中获取数据“x”或“y”?还是“房子”中的“名称”和“年份”?
JSONParser parser = new JSONParser();
JSONArray a = (JSONArray) parser.parse(new FileReader("ff.json"));
for (Object o : a)
{
JSONObject person = (JSONObject) o;
JSONObject coor = (JSONObject) o;
String name = (String) person.get("name");
System.out.println(name);
Long id = (Long) person.get("id");
System.out.println(id);
Double area = (Double) person.get("area");
System.out.println(area);
Coordinates oor = (Coordinates) person.get("coordinates");
System.out.println(person.get("oor"));
我试图这样做,但我得到了异常 " 平五 387 332.3 线程“main”java.lang.ClassCastException 中的异常:org.json.simple.JSONObject 无法转换为坐标”
【问题讨论】:
-
这只是你的手写错误还是json内容就是你上面发布的确切内容?我可以看到你在最后漏掉了一个“}”。