java – 改造和杰克逊和解析JSON

前端之家收集整理的这篇文章主要介绍了java – 改造和杰克逊和解析JSON前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在和杰克逊一起使用Retrofit.由于某些原因我无法解析以下 JSON
[
    {
        "ProfileImage": null,"UserName": "joe"
    },{
        "ProfileImage": "http://www.example.com/profiles/fileName1.jpg","UserName": "jane"
    },{
        "ProfileImage": null,"UserName": "john"
    }
]

我得到这个例外:

Exception in thread "main" retrofit.RetrofitError: java.lang.ClassCastException: 
    sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl 
    cannot be cast to java.lang.Class

at retrofit.RetrofitError.unexpectedError(RetrofitError.java:41)
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:294)
at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:182)
at com.sun.proxy.$Proxy5.getReplies(Unknown Source)
at Test.main(Test.java:25)

Caused by: java.lang.ClassCastException: 
    sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl 
    cannot be cast to java.lang.Class

at JacksonConverter.fromBody(JacksonConverter.java:27)
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:274)

我的代码如下:

// File:Test.java:

public class Test { 
    public static void main(String[] args) {

        ObjectMapper mapper = new ObjectMapper();
        mapper.setPropertyNamingStrategy(new PropertyNamingStrategy
                                                 .PascalCaseStrategy());

        RestAdapter restAdapter = new RestAdapter.Builder()
                                      .setServer("http://www.example.com")
                                      .setConverter(new JacksonConverter(mapper))
                                      .build();

        ListingService listingService = restAdapter.create(ListingService.class);
        ArrayList<ListingReplyExt> replies = listingService.getReplies(123,1,10);
        //More code...
}

}

// File:ListingService.java

public interface ListingService {       
    @GET("/api/ListingRepliesService.svc/Get")
    public ArrayList<ListingReplyExt> getReplies(
                                           @Query("listingId") long listingId,@Query("start") int start,@Query("count") int count);
}

// File:ListingReplyExt.java:

@JsonIgnoreProperties(ignoreUnknown = true)
public class ListingReplyExt {

    private String profileImage;
    private String username;

    @JsonProperty(value = "ProfileImage")
    public String getProfileImage() {
        return profileImage;
    }

    @JsonProperty(value = "ProfileImage")
    public void setProfileImage(String profileImage) {
        this.profileImage = profileImage;
    }

    @JsonProperty(value = "UserName")
    public String getUsername() {
        return username;
    }

    @JsonProperty(value = "UserName")
    public void setUsername(String username) {
        this.username = username;
    }
}

任何帮助都是合法的.

UPDATE

Sotirios Delimanolis的评论给了我一些提示解决这个问题.

我的JacksonConverter原来是这样的:

public class JacksonConverter implements Converter {
    private final ObjectMapper objectMapper;

    public JacksonConverter(ObjectMapper objectMapper) {
        this.objectMapper = objectMapper;
    }

    @Override public Object fromBody(TypedInput body,Type type) {
        return objectMapper.readValue(body.in(),(Class<Object>) type);
    }

    //More code
}

我改为:

public class JacksonConverter implements Converter {
    private final ObjectMapper objectMapper;

    public JacksonConverter(ObjectMapper objectMapper) {
        this.objectMapper = objectMapper;
    }

    @Override public Object fromBody(TypedInput body,Type type) {
        JavaType javaType = objectMapper.getTypeFactory().constructType(type);
        return objectMapper.readValue(body.in(),javaType);
    }

    //More code
}

现在它适用于我

解决方法

使用Square的转换器将其添加到您的build.gradle中:
compile 'com.squareup.retrofit:converter-jackson:1.9.0'

之后,您可以通过将.setConverter(新的JacksonConverter())添加到RestAdapter构建器来激活它.

原文链接:https://www.f2er.com/java/123854.html

猜你在找的Java相关文章