Using JSON : JSON « Development « Android






Using JSON

   
package app.test;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class Test extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    String JSONData = ""
        + "{\"results\":{\"aname\":\"value\", \"anumber\":1234, \"aboolean\":false, "
        + "\"anarray\":[{\"arrayelement\":\"Array Element 1\"}, {\"arrayelement\":\"Array Element 2\"}]}}";

    try {
      JSONObject overallJSONObject = new JSONObject(JSONData);
      Log.v("SIMPLEJSON", overallJSONObject.toString());

      JSONObject resultsObject = overallJSONObject
          .getJSONObject("results");
      Log.v("SIMPLEJSON", resultsObject.toString());

      String aname = resultsObject.getString("aname");
      Log.v("SIMPLEJSON", aname);

      int anumber = resultsObject.getInt("anumber");
      Log.v("SIMPLEJSON", "" + anumber);

      boolean aboolean = resultsObject.getBoolean("aboolean");
      Log.v("SIMPLEJSON", "" + aboolean);

      JSONArray anarray = resultsObject.getJSONArray("anarray");
      for (int i = 0; i < anarray.length(); i++) {
        JSONObject arrayElementObject = anarray.getJSONObject(i);
        String arrayelement = arrayElementObject
            .getString("arrayelement");
        Log.v("SIMPLEJSON", arrayelement);
      }
    } catch (JSONException e) {
      e.printStackTrace();
    }
  }
}

   
    
    
  








Related examples in the same category

1.Parse Json string
2.Upload Image to Flickr with JSON
3.Flickr JSON Location
4.Json Client
5.Get List From Json Object
6.Create JSONObject
7.JSON: Get node value,
8.Data type conversion for JSON
9.Object to JSON
10.Wikidictionary
11.Browse through Wiktionary content
12.Utility class supporting the Facebook Object.