【问题标题】:Parse flat json into array with categories将平面 json 解析为具有类别的数组
【发布时间】:2023-03-22 07:17:02
【问题描述】:

我需要动态解析这个扁平的 JSON:

    [{"titulo":"prueba1","nombrelinea":"cortecaballero","monto":"12"},
    {"titulo":"prueba1","nombrelinea":"cortedama","monto":"15"},
    {"titulo":"prueba1","nombrelinea":"corteniño","monto":"10"},
    {"titulo":"prueba2","nombrelinea":"tintecaballero","monto":"12"},
    {"titulo":"prueba2","nombrelinea":"tintedama","monto":"15"},
    {"titulo":"prueba2","nombrelinea":"tinteniño","monto":"10"},
    {"titulo":"prueba3","nombrelinea":"secadocaballero","monto":"12"},
    {"titulo":"prueba3","nombrelinea":"secadodama","monto":"15"},
    {"titulo":"prueba3","nombrelinea":"secadoniño","monto":"10"}]

这样放入数组中:

 ArrayList<Item> items = new ArrayList<Item>();
     items.add(new SectionItem("prueba 1"));
     items.add(new EntryItem("cortecaballero", "12"));
     items.add(new EntryItem("cortedama", "15"));
     items.add(new EntryItem("corteniño", "12"));
     items.add(new SectionItem("prueba 2"));
     items.add(new EntryItem("tintecaballero", "12"));
     items.add(new EntryItem("tintedama", "15"));
     items.add(new EntryItem("tinteniño", "12"));
     items.add(new SectionItem("prueba 3"));
     items.add(new EntryItem("secadocaballero", "12"));
     items.add(new EntryItem("secadodama", "15"));
     items.add(new EntryItem("secadoniño", "12"));

我试过了,但“titulo”总是在每一行重复。

【问题讨论】:

    标签: java android json listview arraylist


    【解决方案1】:

    好吧,最好更改JSON 的格式,但如果您没有机会这样做并且响应的结构始终与上述相同,则可能的解决方案是以下(假设outJSON 类型为String 的响应):

    ArrayList<Item> items = new ArrayList<Item>();
    JSONArray jsonArray = new JSONArray(out);
    JSONObject jsonObject = new JSONObject();
    Set<String> tempSet = new HashSet<String>();
    for (int i = 0; i<jsonArray.length();i++){
        jsonObject = jsonArray.getJSONObject(i);
        if (!tempSet.contains(jsonObject.getString("titulo"))){
            items.add(new SectionItem(jsonObject.getString("titulo")));
            tempSet.add(jsonObject.getString("titulo"));
        }
        items.add(new EntryItem(jsonObject.getString("nombrelinea"), jsonObject.getString("monto")));
    }
    

    附:进行了编辑以反映 cmets 中描述的问题

    【讨论】:

    • 感谢您的快速回答!我忘了说:JSON 来自数据库的字符串,它的大小可能会有所不同。我告诉你这是因为你的答案是固定大小的 json。再次感谢!
    • 我测试了您的解决方案,但仅在 json 是静态的情况下才有效,但对象的数量会有所不同。您能帮我提供一个动态解决方案吗?
    • @eligdt 你能再举一个你得到的json例子吗?
    • 好的,这是另一个例子! [{"titulo":"prueba1","nombrelinea":"cortecaballero","monto":"12"}, {"titulo":"prueba1","nombrelinea":"cortedama","monto":"15 "}, {"titulo":"prueba1","nombrelinea":"corteniño","monto":"10"}, {"titulo":"prueba2","nombrelinea":"tintecaballero","monto": "12"}, {"titulo":"prueba2","nombrelinea":"tintedama","monto":"15"}, {"titulo":"prueba3","nombrelinea":"tinteniño","monto ":"10"}, {"titulo":"prueba3","nombrelinea":"secadocaballero","monto":"12"}, {"titulo":"prueba4","nombrelinea":"secadodama", "monto":"15"},]
    • 共享相同“titulo”数据的对象数量永远不会相同,它可能会有所不同。
    猜你喜欢
    • 1970-01-01
    • 2013-12-25
    • 2015-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-29
    相关资源
    最近更新 更多