如何对嵌套的 JSON 数组进行排序?

编程入门 行业动态 更新时间:2024-10-11 19:22:30
本文介绍了如何对嵌套的 JSON 数组进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何对嵌套的 JSON 数组进行排序?就像下面的 JSON...

How can i sort Nested JSON Array? Like for a JSON below...

{ "id":"rtmc05.lax.someabc", "name":"rtmc05.lax.someabc", "tenants":[{ "id":"rtmc", "name":"rtmc" },{ "id":"hrs", "name":"hrs" },{ "id":"amotelbe1", "name":"amotelbe" },{ "id":"cds", "name":"cds" },{ "id":"idx-server", "name":"idx-server", "tenants":[{ "id":"amotelbe", "name":"amotelbe", "tenants":[{ "id":"amotelui", "name":"amotelui" }] }] }] }

推荐答案

您的问题隐含了一些部分,并且不清楚您在哪里遇到问题:

There's a few parts implicit to your question, and it's not clear where you're having trouble:

  • 如何获取 JSON 字符串并从中生成可用的 Java 对象.(我假设是 Java,而不是 JavaScript,因为您已经用java"标记了您的问题.)
  • 这些物品制作完成后如何分类?
  • 您如何处理嵌套部分的排序?(在您的示例中,idx-server"有子租户.)
  • 不确定您在这方面的哪些部分遇到了问题,所以这里有针对这三个部分的一些注意事项.

    Not sure exactly which parts of this you're having trouble with, so here's some notes for all three.

    第 1 部分:获取 Java 对象

    Part 1: Getting Java objects

    我同意另一个人的看法,Jackson 是一个很好的 JSON 解析器.这里有几行代码可以用来解析一些 JSON:

    I agree with the other guy that Jackson is a good JSON parser to use. Here's a couple lines of code you could use to parse some JSON:

    String jsonString = "..."; // Load this in whatever way makes sense for you ObjectMapper mapper = new ObjectMapper(); Map<String,Object> parsedJson = mapper.readValue(jsonString, Map.class);

    如果您的 JSON 字符串真的很大,那么您可以使用其他 readValue 重载来避免将整个字符串读入内存.

    If your JSON string is really huge, then there are other readValue overloads that you can use to avoid reading the whole String into memory.

    第 2 部分:对 Java 对象进行排序

    Part 2: Sorting Java objects

    一旦你得到解析的 JSON,排序就只是调用 Collections.sort(...),传入租户 数组.另外,您需要编写一个 Comparator定义您想要的排序.例如,这是一个按名称排序的比较器:

    Once you've got the parsed JSON, sorting is just a matter of calling Collections.sort(...), passing in the tenants array. Plus you'll need to write a Comparator that defines the ordering that you want. For example, here's a comparator that sorts by name:

    public class NameComparator implements Comparator<Map<String,Object>> { public int compare(Map<String,Object> o1, Map<String,Object> o2) { String name1 = (String) o1.get("name"); String name2 = (String) o2.get("name"); return name1pareTo(name2); } }

    然后你得到租户数组(Jackson 将它们变成 ArrayList 对象)并调用 Collections.sort(...).例如,

    Then you get the tenants array out (Jackson makes them into ArrayList objects) and call Collections.sort(...). For example,

    List<Map<String,Object>> tenants = (List<Map<String,Object>>) parsedJson.get("tenants"); Collections.sort(tenants, new NameComparator());

    第 3 部分:处理嵌套

    Part 3: Handling the nesting

    最清晰的方法是添加一些额外的代码来遍历您的 JSON 以查找具有租户数组的任何对象,并对其进行排序.例如,这是一个应该执行此操作的递归函数:

    The clearest way to do this is to add some extra code to walk through your JSON looking for any object with a tenants array, and sort it. For example, here's a recursive function that should do it:

    public static void recursiveSortTenants(Map<String,Object> jsonObject) { List<Map<String,Object>> tenants = (List<Map<String,Object>>) jsonObject.get("tenants"); if (tenants != null) { Collections.sort(tenants, new NameComparator()); // For each tenant, see if it has sub-tenants. If so, // call this function again to sort them. for (Map<String,Object> tenant : tenants) { if (tenants.containsKey("tenants")) { recursiveSortTenants(tenant); } } } }

    希望这会有所帮助!

    更多推荐

    如何对嵌套的 JSON 数组进行排序?

    本文发布于:2023-11-26 09:15:39,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1633525.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:嵌套   数组   JSON

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!