繁体   English   中英

如何将facebook响应转换为json格式的字符串?

[英]how to convert facebook response to a string in json format?

我正在尝试从Facebook获取查询响应并将其映射到用户定义的类:

我有以下课程:

class FBStory {
        @Field
        private String application_id;
        @Field
        private String application_name;
        @Field
        private String caption;
        @Field
        private String created_time;
        @Field
        private String description;
        @Field
        private String from;// user_Id
        @Field
        private String message;
        @Field
        private String name;
        @Field("objectId")
        private String object_id;
        @Field
        private String privacy;
        @Field("statusType")
        private String status_type;
        @Field
        private String story;
        @Field("storyId")
        private String id;
        @Field("storyUrl")
        private String link;
        @Field
        private String type;
        @Field
        private String updated_time;
        @Field
        private Set<CommentInfo> comments = new HashSet<CommentInfo>();//set of comment Id
        @Field
        private Set<String> likes = new HashSet<String>();
        @Field("storyTags")
        private Set<String> story_tags = new HashSet<String>();
}

和facebook的查询响应是:

Post[actions=[] application=null attribution=null caption=null comments=Comments[count=0 data=[]] createdTime=Mon Apr 01 08:16:28 IST 2013 description=null from=CategorizedFacebookType[category=null id=100000363414872 metadata=null name=Nitin Thokare type=null] icon=null id=100000363414872_555878751100900 likes=null likesCount=null link=null message=null messageTags={} metadata=null name=null objectId=null picture=null place=null privacy=Privacy[deny=null description=null friends=null networks=null value=] properties=[] source=null to=[] type=status updatedTime=Mon Apr 01 08:16:28 IST 2013 withTags=[]]

我试图使用以下方法将上述响应转换为上述类的对象:

storyObj = gson.fromJson(restFBResponse.toString(), SolrFBStoryDoc.class);

但是对于gson.fromJson() ,第一个参数应该是JSON格式的String。 因此,以上代码行出现错误:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 5

有什么方法可以将restFBResponse转换为正确的Json字符串,然后将其作为第一个参数传递给gson.fromJson()

我发现使用Solr Beans是可行的,但是使用Solr Beans引起了它自己的问题, 这里讨论了其中之一。 因此,为了克服这个问题,我收到了来自Facebook的JsonObject.class格式(即connectionType)而不是Post.class ,如下所示:

data = facebookClient.fetchConnection(connection, t, parameters)

where, 'fetchConnection' is defined as,

<T> Connection<T> fetchConnection(String connection, Class<T> connectionType, Parameter... parameters);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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