Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to parse the following JSON?
    text
    copied!<p>Hi I am new to working on JSON Format Webservices . I have the Json like....</p> <pre><code>{ "meta":{"success":1}, "Countries": [[{"Id":"1","Name":"Gibraltar", "Cities": [[{"Id":"21","Name":"Gibraltar"}]] }, {"Id":"2","Name":"Canada", "Cities": [[{"Id":"22","Name":"Toronto"}, {"Id":"39","Name":"Banff"}]] }, {"Id":"4","Name":"Malta", "Cities": [[{"Id":"37","Name":"Valletta"}]] }, {"Id":"51","Name":"Italy", "Cities": [[{"Id":"24","Name":"Sardinia"}]] }, {"Id":"53","Name":"England", "Cities": [[{"Id":"23","Name":"London"}, {"Id":"38","Name":"Guildford"}, {"Id":"43","Name":"Petersfield"}, {"Id":"44","Name":"Isle of Wight"}]] }, {"Id":"175","Name":"Hungary", "Cities": [[{"Id":"36","Name":"Budapest"}]] } ]] } </code></pre> <p>But I am unable to get the values .. while parsing as json Object.</p> <p>I have tried to get the values like...</p> <pre><code>public class JSONParsing1 extends ListActivity { private static String url = "http://wsapi.vr2020.com/countries.json"; private static final String TAG_META = "meta"; private static final String TAG_SUCCESS = "success"; private static final String TAG_COUNTRIES = "Countries"; private static final String TAG_ID = "Id"; private static final String TAG_NAME = "Name"; private static final String TAG_CITY = "Cities"; private static final String TAG_CITY_ID = "Id"; private static final String TAG_CITY_NANE = "Name"; private String id; private String name; private String city_id; private String city_name; JSONObject meta; JSONArray Countries = null; JSONArray Cities = null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ArrayList&lt;HashMap&lt;String, String&gt;&gt; list = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;(); JSONParser jsonParser = new JSONParser(); JSONObject jsonObject = jsonParser.getJSONFromUrl(url); try { meta = jsonObject.getJSONObject(TAG_META); TextView startText = (TextView) findViewById(R.id.stat_textView); startText.setText(meta.getString(TAG_SUCCESS)); } catch (Exception e) { e.printStackTrace(); } try { Countries = meta.getJSONArray(TAG_COUNTRIES); for (int i = 0; i &lt; Countries.length(); i++) { JSONObject obj = Countries.getJSONObject(i); id = obj.getString(TAG_ID); name = obj.getString(TAG_NAME); JSONArray city = obj.getJSONArray(TAG_CITY); for(int j = 0; j&lt; city.length(); j++){ JSONObject cityobj = city.getJSONObject(j); city_id = cityobj.getString(TAG_CITY_ID); city_name = cityobj.getString(TAG_CITY_ID); } HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;(); map.put(TAG_ID, id); map.put(TAG_NAME, name); list.add(map); } } catch (Exception e) { e.printStackTrace(); } ListAdapter adapter = new SimpleAdapter(this, list, R.layout.list_items, new String[] { TAG_ID, TAG_NAME, }, new int[] { R.id.id_textView, R.id.name_textView, R.id.description_textView }); setListAdapter(adapter); } } </code></pre> <p>And JSON PArser Class is</p> <pre><code>public class JSONParser { static InputStream is = null; static JSONObject jsonObject = null; static String json = ""; public JSONObject getJSONFromUrl(String url) { try { DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); is = httpEntity.getContent(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { BufferedReader reader = new BufferedReader(new InputStreamReader( is, "iso-8859-1"), 8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } is.close(); json = sb.toString(); } catch (Exception e) { Log.e("Buffer Error", "Error converting result " + e.toString()); } try { jsonObject = new JSONObject(json); } catch (JSONException e) { Log.e("JSON Parser", "Error parsing data " + e.toString()); } return jsonObject; } } </code></pre> <p>Can any one share how to Parse the above JSON? Thanks in advance.</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