0

以下のJSONでfasterxmlを使用してJavaオブジェクトを構築しようとしています

    JSON : {"name":"Location Test",
            "location":{
                "coordinates":[10.1234657,10.123467]
            },
            ...
    }

この例外が発生しています:

play.api.Application$$anon$1: Execution exception[[RuntimeException: com.fasterxml.jackson.databind.JsonMappingException:
Can not deserialize instance of double[] out of START_OBJECT token
 at [Source: N/A; line: -1, column: -1] (through reference chain: com.mypackages.models.Place["location"])]]

プレイス クラス :

public class Place{
   private String name;
   private Location location;
   ...
   getters and setters
}

ロケーション クラス :

public class Location{
   private double[] coordinates;

   public Location(double[] coordinates) {
       this.coordinates = coordinates;
   }
   ...
   //getter and setter for coordinate field
} 

誰かが問題の原因を教えてもらえますか?

4

1 に答える 1

2

ロケーション オブジェクトからコンストラクターを削除する必要があります。いただいた情報をもとにサンプルプログラムを作成したところ、正常に動作しました。

ロケーション クラス:

public class Location{
private double[] coordinates;

/**
 * @return the coordinates
 */
public double[] getCoordinates() {
    return coordinates;
}

/**
 * @param coordinates the coordinates to set
 */
public void setCoordinates(double[] coordinates) {
    this.coordinates = coordinates;
 }
} 

場所のクラス:

public class Place{
private String name;
private Location location;
/**
 * @return the name
 */
public String getName() {
    return name;
}
/**
 * @param name the name to set
 */
public void setName(String name) {
    this.name = name;
}
/**
 * @return the location
 */
public Location getLocation() {
    return location;
}
/**
 * @param location the location to set
 */
public void setLocation(Location location) {
    this.location = location;
}


@Override
public String toString() {
    return "Place: " + name + " Location: " + Arrays.toString(location.getCoordinates());
}
}

APP クラス: public class App { public static void main(String[] args) throws IOException {

      //read json file data to String
      byte[] jsonData = Files.readAllBytes(Paths.get("places.txt"));

      //create ObjectMapper instance
      ObjectMapper objectMapper = new ObjectMapper();

      Place place = objectMapper.readValue(jsonData, Place.class);

      System.out.println("Place Object\n"+ place);
    }
}

Places.txt - JSON を含む

{
   "name":"Location Test",
        "location":{
            "coordinates":[10.1234657,10.123467]
   }
}

Maven プロジェクトに次の依存関係を含める必要があります。

<dependency>
   <groupId>com.fasterxml.jackson.core</groupId>
   <artifactId>jackson-databind</artifactId>
   <version>2.6.1</version>
</dependency>
于 2015-08-16T06:44:34.383 に答える