Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you want to go with the built in JSON-classes, you could do something like this:</p> <pre><code>DefaultHttpClient defaultClient = new DefaultHttpClient(); HttpGet httpGetRequest = new HttpGet(s); HttpResponse httpResponse = defaultClient.execute(httpGetRequest); BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(), "UTF-8")); String json = reader.readLine(); JSONObject jsonObject = new JSONObject(json); if (jsonObject.has("lessons")) { JSONArray jsonLessons = jsonObject.getJSONArray("lessons"); List&lt;Lesson&gt; lessons = new ArrayList&lt;Lesson&gt;(); for(int i = 0; i &lt; jsonLessons.length(); i++) { JSONObject jsonLesson = jsonLessons.get(i); // Use optString instead of get on the next lines if you're not sure // the fields are always there String name = jsonLesson.getString("name"); String teacher = jsonLesson.getString("prof"); lessons.add(new Lesson(name, teacher)); } } </code></pre> <p>Just be sure that your Json always arrives in a single line. Breaking a line would break this code, as you only read the line.</p> <p>My choice would be Gson. In this case you would create a Lesson class and a Schedule class:</p> <pre><code>public class Lesson { String name; String prof; } public class Schedule { List&lt;Lesson&gt; lessons; } </code></pre> <p>Note that the field names corresponds to the json fields. Feel free make the fields private and add som getter methods if that feels better. :-)</p> <p>Now you can parse out the Schedule object containing the lessons list with:</p> <pre><code>DefaultHttpClient defaultClient = new DefaultHttpClient(); HttpGet httpGetRequest = new HttpGet(s); HttpResponse httpResponse = defaultClient.execute(httpGetRequest); Reader in = new InputStreamReader(httpResponse.getEntity().getContent(), "UTF-8"); Gson gson = new Gson(); Schedule schedule = gson.fromJson(in, Schedule.class); List&lt;Lesson&gt; lessons = schedule.lessons; </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