gpt4 book ai didi

android - 如果退出相同的值android studio如何对json数组数据进行分组

转载 作者:行者123 更新时间:2023-11-29 19:31:11 24 4
gpt4 key购买 nike

我有这样的 Json,我想按 sub_name 分组以获取电话数组,移动操作(每个数组具有相同的数据值只获取数组中的一个)。

 category: {
cate: [
{
sub_id: "568",
sub_name: "Phones",
ns_id: "17002",
ns_name: "Mobile Phone"
},
{
sub_id: "568",
sub_name: "Phones",
ns_id: "18787",
ns_name: "Other Phones"
},
{
sub_id: "372",
sub_name: "Mobile Operation",
ns_id: "3650",
ns_name: "Sim Card"
}
}

最佳答案

在你的情况下,试试

String subName = "Phones";
JSONObject json = new JSONObject(jsonString);
JSONArray jsonArray = json.getJSONArray("cate");
JSONArray groupJsonWithSubName = new JSONArray();

for(int i=0; i < jsonArray.length(); i++){
JSONObject jsonData = jsonArray.getJSONObject(i);

if(jsonData.getString("sub_name").equals(subName){
groupJsonWithSubName.put(jsonData);
}
}
// in here groupJsonWithSubName will contains all json object which have sub_name="Phones"

关于android - 如果退出相同的值android studio如何对json数组数据进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39911460/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com