2

私はJSONを持っています:

{
    "firstField": "Something One",
    "secondField": "Something Two",
    "thirdField": [
        {
            "thirdField_one": "Something Four",
            "thirdField_two": "Something Five"
        },
        {
            "thirdField_one": "Something Six",
            "thirdField_two": "Something Seven"
        }
    ],
    "fifthField": [
        {
            "fifthField_one": "Something… ",
            "fifthField_two": "Something...",
            "fifthField_three": 12345
        },
        {
            "fifthField_one": "Something",
            "fifthField_two": "Something",
            "fifthField_three": 12345
        }
    ]
}

私は私のクラスを持っています:

public static class MyClass {
        @JsonProperty
        private String firstField, secondField;
        @JsonProperty
        private ThirdField thirdField;
        @JsonProperty
        private FifthField fifthField;

        public static class ThirdField {
            private List<ThirdFieldItem> thirdField;
        }

        public static class ThirdFieldItem {
            private String thirdField_one, thirdField_two;
        }

        public static class FifthField {
            private List<FifthFieldItem> fifthField;
        }

        public static class FifthFieldItem {
            private String fifthField_one, fifthField_two;
            private int fifthField_three;
        }
    }

私はジャクソンライブラリでそれらを逆シリアル化しています:

public void testJackson() throws IOException {
    JsonFactory factory = new JsonFactory();
    ObjectMapper mapper = new ObjectMapper(factory);
    File from = new File("text.txt"); // JSON I mentioned above
    mapper.readValue(from, MyClass.class);
}

しかし、私は例外を取得しています:

org.codehaus.jackson.map.JsonMappingException:START_ARRAYトークンからMain $ MyClass$ThirdFieldのインスタンスを逆シリアル化できません

4

2 に答える 2

5

thirdFieldfifthFieldプロパティをJSONの配列として定義しました。これらは、JavaBeanの配列またはコレクションである必要があります。

public static class MyClass {
    @JsonProperty
    private String firstField, secondField;

    @JsonProperty
    private Collection<ThirdField> thirdField;

    @JsonProperty
    private Collection<FifthField> fifthField;

    /// ...
}

既存のJSONオブジェクトを処理してBeanに変換するときは、JSONデータがマップに非常によく似ていることに注意してください。マップからオブジェクトにデータをマップする方法を想像する場合、それは本当に役立ちます。ThirdFieldとオブジェクトは、 JSONFifthFieldの定義をマップする必要があります。これはあなたのJSONが言うことThirdFieldです:

{
    "thirdField_one": "Something Four",
    "thirdField_two": "Something Five"
}

文字通りそれをJavaBeanに変換すると、次のようになります。

public class ThirdField implements Serializable {
    private String thirdField_one;
    private String thirdField_two;

    // ...
}

アノテーションなどを追加して、本格的なBeanを取得できます。オブジェクトに対して同じことを行いますFifthField

于 2012-11-28T08:42:30.927 に答える
0

注: 私はEclipseLink JAXB(MOXy)のリーダーであり、JAXB(JSR-222)エキスパートグループのメンバーです。

ドメインモデルを変更できるとは限らないため(@Perceptionによる回答)、MOXyを使用して元のオブジェクトモデルを目的のJSONにマッピングする方法を以下に示します。

Javaモデル

このユースケースでは、@XmlPath(".")拡張機能を活用できます。これは、MOXyにターゲットオブジェクトのコンテンツをソースノードに取り込むように指示します。

@XmlAccessorType(XmlAccessType.FIELD)
public static class MyClass {
    private String firstField, secondField;

    @XmlPath(".")
    private ThirdField thirdField;

    @XmlPath(".")
    private FifthField fifthField;

    @XmlAccessorType(XmlAccessType.FIELD)
    public static class ThirdField {
        private List<ThirdFieldItem> thirdField;
    }

    @XmlAccessorType(XmlAccessType.FIELD)
    public static class ThirdFieldItem {
        private String thirdField_one, thirdField_two;
    }

    @XmlAccessorType(XmlAccessType.FIELD)
    public static class FifthField {
        private List<FifthFieldItem> fifthField;
    }

    @XmlAccessorType(XmlAccessType.FIELD)
    public static class FifthFieldItem {
        private String fifthField_one, fifthField_two;
        private int fifthField_three;
    }
}

変換コード

以下のデモコードは、MOXyのJSONバインディングを有効にする方法を示しています。

public static void main(String[] args) throws Exception {
    Map<String, Object> properties = new HashMap<String, Object>(2);
    properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
    properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
    JAXBContext jc = JAXBContext.newInstance(new Class[] {MyClass.class}, properties);

    Unmarshaller unmarshaller = jc.createUnmarshaller();
    StreamSource json = new StreamSource("src/forum13600952/input.json");
    MyClass myClass = unmarshaller.unmarshal(json, MyClass.class).getValue();

    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(myClass, System.out);
}

input.json / Output

Belosは、MOXyによって生成された出力と一致するように、わずかに再フォーマットされた質問からの入力です。

{
   "firstField" : "Something One",
   "secondField" : "Something Two",
   "thirdField" : [ {
      "thirdField_one" : "Something Four",
      "thirdField_two" : "Something Five"
   }, {
      "thirdField_one" : "Something Six",
      "thirdField_two" : "Something Seven"
   } ],
   "fifthField" : [ {
      "fifthField_one" : "Something...",
      "fifthField_two" : "Something...",
      "fifthField_three" : 12345
   }, {
      "fifthField_one" : "Something",
      "fifthField_two" : "Something",
      "fifthField_three" : 12345
   } ]
}

詳細については

于 2012-11-28T11:22:12.157 に答える