Converting Json to java objects using Google's Gson

情到浓时终转凉″ 提交于 2019-12-01 16:56:05

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<JsonHelper> json = new ArrayList<JsonHelper>();
    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());
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!