如何从响应中获得计数值?

时间:2018-07-26 05:52:54

标签: json retrofit

{
    "count": 1,
    "next": null,
    "previous": null,
    "results": [
        {
            "likecount": 0,
            "unlikeCount": 0,
            "shareCount": 0,
            "i_id": 297,
            "p_id": 297,
            "name": "Airport  runways  columbia  hon",
            "description": "Airport  runways  columbia  hongkong  aviation  airplanes  aeroplanes",
            "c_id": 1,
            "status_id": 1,
            "source_id": 149,
            "date_created": null,
            "parentinfograph": "Staffing insurance  liability  ",
            "internal_url": "http://167.99.86.81:8000/media/DQ297.jpg",
            "external_url": "http://www.aertecsolutions.com/wp-content/uploads/2017/03/Infographic-Runway-ENG.jpg",
            "mt_id": 1,
            "mtcode": "Business"
        }
    ]
}

2 个答案:

答案 0 :(得分:0)

首先,使用JSON字符串创建一个POJO类:

http://www.jsonschema2pojo.org/

然后,创建POJO类的实例。现在,使用GSON,您可以在实例中设置值。最后,您可以使用getter方法获取值

例如。

Usermodel usermodel = new Gson().fromJson(<JSON string>, UserModel.class);

Usermodel是我的POJO类。

答案 1 :(得分:0)

Please check below:

    @Test
    public void getlikecount() {
        String JSON = "{\r\n" + 
                "    \"count\": 1,\r\n" + 
                "    \"next\": null,\r\n" + 
                "    \"previous\": null,\r\n" + 
                "    \"results\": [\r\n" + 
                "        {\r\n" + 
                "            \"likecount\": 0,\r\n" + 
                "            \"unlikeCount\": 0,\r\n" + 
                "            \"shareCount\": 0,\r\n" + 
                "            \"i_id\": 297,\r\n" + 
                "            \"p_id\": 297,\r\n" + 
                "            \"name\": \"Airport  runways  columbia  hon\",\r\n" + 
                "            \"description\": \"Airport  runways  columbia  hongkong  aviation  airplanes  aeroplanes\",\r\n" + 
                "            \"c_id\": 1,\r\n" + 
                "            \"status_id\": 1,\r\n" + 
                "            \"source_id\": 149,\r\n" + 
                "            \"date_created\": null,\r\n" + 
                "            \"parentinfograph\": \"Staffing insurance  liability  \",\r\n" + 
                "            \"internal_url\": \"http://167.99.86.81:8000/media/DQ297.jpg\",\r\n" + 
                "            \"external_url\": \"http://www.aertecsolutions.com/wp-content/uploads/2017/03/Infographic-Runway-ENG.jpg\",\r\n" + 
                "            \"mt_id\": 1,\r\n" + 
                "            \"mtcode\": \"Business\"\r\n" + 
                "        }\r\n" + 
                "    ]\r\n" + 
                "}";

         JsonPath jsonpath = new JsonPath(JSON);
         System.out.println("value of key->likecount: "+jsonpath.getString("results.likecount[0]"));



    }

Output: value of key->likecount: 0