如何使用andorid从json响应中检索唯一(删除重复项)结果

时间:2019-07-05 10:22:08

标签: android json

我需要从json网络服务中检索数据,我已经读取了json中的所有数据。但是我需要从json存储唯一值。

是否可以从json获取唯一值?

以下是我的JSON数据。

{“成功”:1,“数据”:[ {“ unit_no”:1,“ unit_name”:“简介”,“ unitpointno”:“ 1.1”,“ unitpointname”:“ aa”},{“ unit_no”:1,“ unit_name”:“简介”,“ unitpointno” :“ 1.2”,“ unitpointname”:“ bb”},{“ unit_no”:1,“ unit_name”:“简介”,“ unitpointno”:“ 1.3”,“ unitpointname”:“ cc”},{“ unit_no” :2,“ unit_name”:“法律”,“ unitpointno”:“ 2.1”,“ unitpointname”:“ ww”},{“ unit_no”:2,“ unit_name”:“法律”,“ unitpointno”:“ 2.2” ,“ unitpointname”:“ www”},{“ unit_no”:2,“ unit_name”:“法律”,“ unitpointno”:“ 2.3”,“ unitpointname”:“ wwww”},{“ unit_no”:2,“ unit_name“:”法律“,” unitpointno“:” 2.4“,” unitpointname“:” wwww“}]}

但是,我要关注输出(只需删除重复的值,即简介和法律)

operator()

2 个答案:

答案 0 :(得分:0)

您可以修改JSON输出以仅返回唯一值。

像这样使用:

{
"success": 1,
"data": [{
            "unit_no": 1,
            "unit_name": "Introduction",
            "unit_point": [{

                "unitpointno": "1.1",
                "unitpointname": "aa"
            }, {

                "unitpointno": "1.2",
                "unitpointname": "bb"
            }, {

                "unitpointno": "1.3",
                "unitpointname": "cc"
            }]
        },
        {
            "unit_no": 2,
            "unit_name": "Laws",
            "unit_point ": [{

                    "unitpointno": "2.1",
                    "unitpointname": "ww"
                },
                {
                    "unitpointno": "2.2",
                    "unitpointname": "www"
                },
                {
                    "unitpointno": "2.3",
                    "unitpointname": "wwww"
                },
                {
                    "unitpointno": "2.4",
                    "unitpointname": "wwww"
                }]
        }]
}

答案 1 :(得分:0)

Finally I got the solution, I simply did the following code...

if(pid==0 && i==0) 
{  
    pid=Integer.parseInt(jsonObject.getString("unit_no"));
    tlist.setUnitno(unit_no);
    tlist.setUnitName(unit_name);

}   
else  
{  
    nid=Integer.parseInt(jsonObject.getString("unit_no"));
    if(pid==nid) 
    {
         tlist.setUnitno("");
         tlist.setUnitName("");

    }
    else
    {
         tlist.setUnitno(unit_no);
         tlist.setUnitName(unit_name);
         pid=Integer.parseInt(jsonObject.getString("unit_no"));

     }
 }