Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The most straightforward approach I can think of is to just treat the structure as a <code>Map</code> (of <code>Map</code>).</p> <p>With Gson, this is relatively easy to do, as long as the <code>Map</code> structure is statically known, every branch from the root has the same depth, and everything is a <code>String</code>.</p> <pre><code>import java.io.FileReader; import java.lang.reflect.Type; import java.util.Map; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; public class GsonFoo { public static void main(String[] args) throws Exception { Gson gson = new Gson(); Type mapType = new TypeToken&lt;Map&lt;String,Map&lt;String, String&gt;&gt;&gt;() {}.getType(); Map&lt;String,Map&lt;String, String&gt;&gt; map = gson.fromJson(new FileReader("input.json"), mapType); System.out.println(map); // Get the count... int count = Integer.parseInt(map.get("0").get("count")); // Get each numbered entry... for (int i = 1; i &lt;= count; i++) { System.out.println("Entry " + i + ":"); Map&lt;String, String&gt; numberedEntry = map.get(String.valueOf(i)); for (String key : numberedEntry.keySet()) System.out.printf("key=%s, value=%s\n", key, numberedEntry.get(key)); } // Get the routes... Map&lt;String, String&gt; routes = map.get("routes"); // Get each route... System.out.println("Routes:"); for (String key : routes.keySet()) System.out.printf("key=%s, value=%s\n", key, routes.get(key)); } } </code></pre> <p>For more dynamic <code>Map</code> structure handling, I strongly suggest switching to use <a href="http://jackson.codehaus.org" rel="noreferrer">Jackson</a>, instead of Gson, as Jackson will deserialize any JSON object of any arbitrary complexity into a Java <code>Map</code>, with just one simple line of code, and it will automatically retain the types of primitive values.</p> <pre><code>import java.io.File; import java.util.Map; import org.codehaus.jackson.map.ObjectMapper; public class JacksonFoo { public static void main(String[] args) throws Exception { ObjectMapper mapper = new ObjectMapper(); Map map = mapper.readValue(new File("input.json"), Map.class); System.out.println(map); } } </code></pre> <p>The same can be achieved with Gson, but it requires dozens of lines of code. (Plus, Gson has other shortcomings that make switching to Jackson well worth it.)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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