0

我正在获取 json 原始数据,并且在主类中对其进行了很好的解析。但是我不知道如何编写一个 pojo 来从我正在解析的 json 原始数据中获取所有值。这个类得到原始 json 并解析所需的字段我想写一个 pojo 来获取所有这些字段。

public class FaceBookparser {


public static void main(String[] args) throws FacebookException, JSONException, IOException {

    // Generate facebook instance.
    Facebook facebook = new FacebookFactory().getInstance();
    // Use default values for oauth app id.
    facebook.setOAuthAppId("APPID", "APP-SEC-ID");

    AccessToken accessTokenString = facebook.getOAuthAppAccessToken();

  facebook.setOAuthAccessToken(accessTokenString);
    StringBuilder sb = new StringBuilder();

     String z="";
   String m ="AnushkaShetty/?fields=posts.limit(2).since(2015).until(now){id,message,name,type,picture,link,caption,description,icon,application,shares,updated_time,source,comments.limit(500).summary(true){comment_count,message,can_remove,id,created_time,can_like,like_count,comments{comment_count,comments{comment_count}}},place,object_id,privacy,status_type,created_time,story,parent_id,story_tags,full_picture,likes.limit(9999).summary(true){id,name,username}},id,hometown,website,about,location,birthday,name,tagged{message_tags},category,category_list,talking_about_count,likes";
            //RawAPIResponse res = facebook.callGetAPI(n);//"me/friends"
            RawAPIResponse res1 = facebook.callGetAPI(m);//"me/friends"
           JSONObject mainobject= res1.asJSONObject();

           System.out.println(mainobject); //print mainjson object

           //page details with in the mainjson
           String id=mainobject.getString("id");

           String birthday=mainobject.getString("birthday");

           String category=mainobject.getString("category");

           String website=mainobject.getString("website");

           String Page_likes=mainobject.getString("likes");

           String name=mainobject.getString("name");

           String about=mainobject.getString("about");

           String talking_about_count=mainobject.getString("talking_about_count");

         /*  System.out.println("id:   "+id);
           System.out.println("birthday:   "+birthday);
           System.out.println("category:   "+category);
           System.out.println("website:   "+website);
           System.out.println("website:   "+Page_likes);
           System.out.println("name:  "+name);
           System.out.println("about:  "+about);
           System.out.println("talking_about_count:  "+talking_about_count);*/

           //location details of the page

           JSONObject location=mainobject.getJSONObject("location");
           String page_country=location.getString("country");
           String page_city=location.getString("city");

       /*    System.out.println("country :"+page_country);
           System.out.println("page_city :"+page_city); */

            JSONObject posts = mainobject.getJSONObject("posts");

            JSONArray data = posts.getJSONArray("data");

            for(int i=0;i<data.length();i++){

            JSONObject dataobjs = data.getJSONObject(i);

            String full_picture=dataobjs.getString("full_picture");
            System.out.println("full_picture "+full_picture);
            String link=dataobjs.getString("link");
            System.out.println("link "+link);
            String type=dataobjs.getString("type");
            System.out.println("type "+type);
            String updated_time=dataobjs.getString("updated_time");
            System.out.println("updated_time "+updated_time);
            String postid=dataobjs.getString("id");
            System.out.println("post_id "+postid);
            String postmessage=dataobjs.getString("message");
            System.out.println("post_message "+postmessage);
            String postpicture=dataobjs.getString("picture");
            System.out.println("post_picture  "+postpicture);
            try{
            String poststory=dataobjs.getString("story");
            // System.out.println("story   "+poststory);
            String postsource=dataobjs.getString("source");
            ArrayList<String> elements = new ArrayList<>();
            System.out.println("post_source  "+postsource);
            System.out.println("**"+elements);
            String postdescription=dataobjs.getString("description");
            System.out.println("postdescription  "+postdescription);

            String statustype=dataobjs.getString("status_type");
            System.out.println("status_type  "+statustype);
            String postname=dataobjs.getString("name");
            System.out.println("postname "+postname);
            String postcreatedat=dataobjs.getString("created_time");
            System.out.println("postcreatedat  "+postcreatedat);
            String postparentid=dataobjs.getString("parent_id");
            System.out.println("parent_id "+postparentid);
            }catch(Exception e ){System.err.println();}
            //privacy object 
            JSONObject privacy=dataobjs.getJSONObject("privacy");

            for(int pri=0;pri<privacy.length();pri++){

                String friends=privacy.getString("friends");
                String allow=privacy.getString("allow");
                String deny=privacy.getString("deny");
                String privacydescription=privacy.getString("description");
                String value=privacy.getString("value");
            }

            //story_tags

            try{

            JSONArray storytags=dataobjs.getJSONArray("story_tags");
            for(int story=0;story<storytags.length();story++)
            {
                JSONObject storydata=storytags.getJSONObject(story);
                String storyid=storydata.getString("id");
                try{
                String storyname=storydata.getString("name");
                System.out.println(" same to same "+storyname);
                }catch(Exception e ){String storyname="dummie";}
                String storylength=storydata.getString("length");
                String storyoffset=storydata.getString("offset");
                String storytype=storydata.getString("type");

                System.out.println("same to same "+id);
            }
            }catch(Exception e ){  System.err.println();}

    }//Main_close
}//class_close
4

0 回答 0