如何在java中编写Json pojo类?

时间:2016-01-29 05:30:58

标签: java json facebook-graph-api facebook4j

我正在获取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

0 个答案:

没有答案