【问题标题】:how to convert facebook response to a string in json format?如何将 facebook 响应转换为 json 格式的字符串?
【发布时间】:2013-04-01 05:32:47
【问题描述】:

我正在尝试从 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 格式的字符串。因此,我收到上述代码行的错误:

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

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

【问题讨论】:

    标签: java json facebook-graph-api gson restfb


    【解决方案1】:

    我发现使用 Solr Beans 是可能的,但是使用 Solr Beans 会产生它自己的问题,其中一个问题已在 here 中讨论过。所以,为了克服这个问题,我收到了来自 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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-01
      • 1970-01-01
      • 2012-10-14
      • 2017-01-17
      • 1970-01-01
      • 2013-02-04
      • 2020-09-09
      相关资源
      最近更新 更多