简体   繁体   中英

I can't get data of object in json

I have json file with an object

{ 
"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
}

so, how can I get data "x" or "y" for example from "coordinates"? or "name" and "year" from "house"?

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

I tried to do this but I get exception " flatFive 387 332.3 Exception in thread "main" java.lang.ClassCastException: org.json.simple.JSONObject cannot be cast to Coordinates"

I think using jackson for this would be a better approach. Something like this:

在此处输入图片说明

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;

public class Parse {

    public static void main(String[] args) {
        parse("{\n" + "   \"id\":387,\n" + "   \"name\":\"flatFive\",\n" + "   \"coordinates\":{\n" + "      \"x\":9.6,\n" + "      \"y\":2.2\n" + "   },\n" + "   \"creationDate\":{\n" + "      \"year\":2020,\n" + "      \"monthValue\":4,\n" + "      \"month\":\"APRIL\",\n" + "      \"dayOfMonth\":1,\n" + "      \"dayOfYear\":92,\n" + "      \"dayOfWeek\":\"WEDNESDAY\",\n" + "      \"hour\":20,\n" + "      \"minute\":40,\n" + "      \"second\":47,\n" + "      \"nano\":662000000,\n" + "      \"chronology\":{\n" + "         \"id\":\"ISO\",\n" + "         \"calendarType\":\"iso8601\"\n" + "      }\n" + "   },\n" + "   \"area\":332.3,\n" + "   \"numberOfRooms\":3,\n" + "   \"furnish\":\"bad\",\n" + "   \"view\":\"NORMAL\",\n" + "   \"transport\":\"NONE\",\n" + "   \"house\":{\n" + "      \"name\":\"Cottage\",\n" + "      \"year\":3,\n" + "      \"numberOfLifts\":6\n" + "   }\n" + "}");
    }

    public static void parse(String url) {
        Destination destination = (Destination) convertBodyToObject(url, Destination.class);
        System.out.println(destination.getId());
        System.out.println(destination.getCoordinates().getX());
        System.out.println(destination.getCoordinates().getY());
        System.out.println(destination.getHouse().getName());
        System.out.println(destination.getHouse().getYear());
    }

    public static Object convertBodyToObject(String body, Class object) {
        try {
            return new ObjectMapper().readValue(body, object);
        } catch (JsonGenerationException e) {
            System.out.println("JsonGenerationException - Failed to convertBodyToObject() " + e);
        } catch (JsonMappingException e) {
            System.out.println("JsonMappingException - Failed to convertBodyToObject() " + e);
        } catch (IOException e) {
            System.out.println("IOException - Failed to convertBodyToObject() " + e);
        }
        throw new RuntimeException("Terminating... " + body + " couldn't be mapped correctly");
    }
}


import com.fasterxml.jackson.annotation.JsonIgnoreProperties;


@JsonIgnoreProperties(ignoreUnknown = true)
public class Destination {
    private Integer id;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    private Coordinates coordinates;

    private House house;

    public Coordinates getCoordinates() {
        return coordinates;
    }

    public void setCoordinates(Coordinates coordinates) {
        this.coordinates = coordinates;
    }

    public House getHouse() {
        return house;
    }

    public void setHouse(House house) {
        this.house = house;
    }
}

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true)
public class House {
    private String name;
    private Integer year;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getYear() {
        return year;
    }

    public void setYear(Integer year) {
        this.year = year;
    }
}

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true)
public class Coordinates {
    private Double x;
    private Double y;

    public Double getX() {
        return x;
    }

    public void setX(Double x) {
        this.x = x;
    }

    public Double getY() {
        return y;
    }

    public void setY(Double y) {
        this.y = y;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM