Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There's a few parts implicit to your question, and it's not clear where you're having trouble:</p> <ol> <li>How do you take a JSON string and make usable Java objects out of it. (I'm assuming Java, not JavaScript, since you've tagged your question with "java".)</li> <li>How do you sort those objects after they're made?</li> <li>How do you handle sorting the nested parts? (In your example, "idx-server" has sub-tenants.)</li> </ol> <p>Not sure exactly which parts of this you're having trouble with, so here's some notes for all three.</p> <p>Part 1: Getting Java objects</p> <p>I agree with the other guy that <a href="http://jackson.codehaus.org/" rel="nofollow">Jackson</a> is a good JSON parser to use. Here's a couple lines of code you could use to parse some JSON:</p> <pre><code>String jsonString = "..."; // Load this in whatever way makes sense for you ObjectMapper mapper = new ObjectMapper(); Map&lt;String,Object&gt; parsedJson = mapper.readValue(jsonString, Map.class); </code></pre> <p>If your JSON string is really huge, then there are other <code>readValue</code> overloads that you can use to avoid reading the whole String into memory.</p> <p>Part 2: Sorting Java objects</p> <p>Once you've got the parsed JSON, sorting is just a matter of calling <a href="http://docs.oracle.com/javase/6/docs/api/java/util/Collections.html#sort%28java.util.List,%20java.util.Comparator%29" rel="nofollow"><code>Collections.sort(...)</code></a>, passing in the <code>tenants</code> array. Plus you'll need to write a <a href="http://docs.oracle.com/javase/6/docs/api/java/util/Comparator.html" rel="nofollow">Comparator</a> that defines the ordering that you want. For example, here's a comparator that sorts by name:</p> <pre><code>public class NameComparator implements Comparator&lt;Map&lt;String,Object&gt;&gt; { public int compare(Map&lt;String,Object&gt; o1, Map&lt;String,Object&gt; o2) { String name1 = (String) o1.get("name"); String name2 = (String) o2.get("name"); return name1.compareTo(name2); } } </code></pre> <p>Then you get the tenants array out (Jackson makes them into <a href="http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html" rel="nofollow"><code>ArrayList</code></a> objects) and call <a href="http://docs.oracle.com/javase/6/docs/api/java/util/Collections.html#sort%28java.util.List,%20java.util.Comparator%29" rel="nofollow"><code>Collections.sort(...)</code></a>. For example,</p> <pre><code>List&lt;Map&lt;String,Object&gt;&gt; tenants = (List&lt;Map&lt;String,Object&gt;&gt;) parsedJson.get("tenants"); Collections.sort(tenants, new NameComparator()); </code></pre> <p>Part 3: Handling the nesting</p> <p>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:</p> <pre><code>public static void recursiveSortTenants(Map&lt;String,Object&gt; jsonObject) { List&lt;Map&lt;String,Object&gt;&gt; tenants = (List&lt;Map&lt;String,Object&gt;&gt;) 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&lt;String,Object&gt; tenant : tenants) { if (tenants.containsKey("tenants")) { recursiveSortTenants(tenant); } } } } </code></pre> <p>Hope this helps!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload