Java Jackson ResourceAccessException:I/O错误:无法识别的字段

Java Jackson ResourceAccessException:I/O错误:无法识别的字段,java,json,jackson,Java,Json,Jackson,我有这个json文件 [ { "foo":{ "comment":null, "media_title":"How I Met Your Mother", "user_username":"nani" } }, { "foo":{ "comment":null, "media_title":"Family Guy", "user_use

我有这个json文件

[
   {
      "foo":{
         "comment":null,
         "media_title":"How I Met Your Mother",
         "user_username":"nani"
      }
   },
   {
      "foo":{
         "comment":null,
         "media_title":"Family Guy",
         "user_username":"nani"
      }
   }
]
所以它是一个Foo实体数组

然后我得到了我的Foo对象:

    import org.codehaus.jackson.annotate.JsonProperty;
    import org.codehaus.jackson.map.annotate.JsonRootName;

    @JsonRootName("foo")
    public class Foo {

        @JsonProperty
        String comment;
        @JsonProperty("media_title")
        String mediaTitle;
        @JsonProperty("user_username")
        String userName;

/** setters and getters go here **/

    }
然后,我的脚模板如下:

public List<Foo> getFoos() {
    return java.util.Arrays.asList(restTemplate.getForObject(buildUri("/foos.json"),
            Foo[].class));
}

异常
表明它正在尝试将
JSONObject
反序列化为
Foo
对象(作为顶级
JSONArray
元素的那些对象)。因此,您没有包含
Foo
实体的数组,而是包含包含
Foo
成员的对象数组

以下是
ObjectMapper
正在尝试做的事情:

[
   {            <---- It thinks this is a Foo.
      "foo":{   <---- It thinks this is a member of a Foo.
         "comment":null,
         "media_title":"How I Met Your Mother",
         "user_username":"nani"
      }
   },
   {            <---- It thinks this is a Foo.
      "foo":{   <---- It thinks this is a member of a Foo.
         "comment":null,
         "media_title":"Family Guy",
         "user_username":"nani"
      }
   }
]
编辑

您也可以创建一个新的
Bar
对象,该对象将保存一个
Foo
实例,并尝试将其解组到该实例的数组中

class Bar {
    @JsonProperty
    private Foo foo;

    // setter/getter
}

public List<Bar> getBars() {
    return java.util.Arrays.asList(restTemplate.getForObject(buildUri("/foos.json"),
            Bar[].class));
}
类栏{
@JsonProperty
私人富福;
//二传手
}
公共列表getbar(){
返回java.util.Arrays.asList(restemplate.getForObject(buildUri(“/foos.json”),
酒吧【】类);
}
[
   {
      "comment":null,
      "media_title":"How I Met Your Mother",
      "user_username":"nani"
   },
   {
      "comment":null,
      "media_title":"Family Guy",
      "user_username":"nani"
   }
]
class Bar {
    @JsonProperty
    private Foo foo;

    // setter/getter
}

public List<Bar> getBars() {
    return java.util.Arrays.asList(restTemplate.getForObject(buildUri("/foos.json"),
            Bar[].class));
}