Converting Json to java objects using Google's Gson

后端 未结 1 655
北荒
北荒 2021-01-18 03:42

I am using Spring Social FqlQuery to get data\'s from facebook. Here is the JSON response I am getting from facebook. My controller where i am getting Json output is here,

1条回答
  •  隐瞒了意图╮
    2021-01-18 04:03

    Since i am not having the actual api access, so i am trying it with static value in the example. Firstly in your JsonHelper class, replace all int by long , as the values mentioned in the json are of type long and String. Then try it like mentioned below:

                String str = "{\n"
                + "  \"employer\": {\n"
                + "    \"id\": 129843057436,\n"
                + "    \"name\": \"www.metroplots.com\"\n"
                + "  },\n"
                + "  \"location\": {\n"
                + "    \"id\": 102186159822587,\n"
                + "    \"name\": \"Chennai, Tamil Nadu\"\n"
                + "  },\n"
                + "  \"position\": {\n"
                + "    \"id\": 108480125843293,\n"
                + "    \"name\": \"Web Developer\"\n"
                + "  },\n"
                + "  \"start_date\": \"2012-10-01\",\n"
                + "  \"end_date\": \"2013-05-31\"\n"
                + "}";
    
    
        List json = new ArrayList();
        Gson gson = new Gson();
        JsonHelper users = gson.fromJson(str, JsonHelper.class);
        json.add(users);
    
    
        for (JsonHelper js_obj : json) {
            System.out.println(js_obj.getEmployer().getId());
            System.out.println(js_obj.getEmployer().getName());
        }
    

    0 讨论(0)
提交回复
热议问题