Android JSON Support
Android already contains the required JSON libraries. The Following class are used to parse the JSON Object.
JSONArray A JSONArray is an ordered sequence of values.
JSONObject A JSONObject is an unordered collection of name/value pairs.
JSONStringer JSONStringer provides a quick and convenient way of producing JSON text.
JSONTokener A JSONTokener takes a source string and extracts characters and tokens from it.
Steps to Parse JSON data
1. Create a JSON Object and Convert JSONString to JSON Object
JSONObject jObject = new JSONObject(jsonString);
2. Create a Key based JSONObject.
JSONObject menuObject = jObject.getJSONObject("menu");
3. Get the Value
Log.d("ID", menuObject.getString("id"));
Log.d("Value ", menuObject.getString("value"));
1> First we’ll need an example :
Lets look at a standard example from the json site http://json.org/example.html
{"menu": { "id": "file", "value": "File", "popup": { "menuitem": [ {"value": "New", "onclick": "CreateNewDoc()"}, {"value": "Open", "onclick": "OpenDoc()"}, {"value": "Close", "onclick": "CloseDoc()"} ] } }}
you could either save this in a file or save it in a string…..like I’ve done
2> Android already contains the required JSON libraries
Lets create a JSON Object;
private JSONObject jObject;and lets our example be a String ,
private String jString = "{\"menu\": {\"id\": \"file\", \"value\": \"File\", \"popup\": { \"menuitem\": [ {\"value\": \"New\", \"onclick\": \"CreateNewDoc()\"}, {\"value\": \"Open\", \"onclick\": \"OpenDoc()\"}, {\"value\": \"Close\", \"onclick\": \"CloseDoc()\"}]}}}";
now we have to convert jString to the jObject ,
jObject = new JSONObject(jString);
Now we have to start extracting the content from jObject ,
Lets extract the menu object by creating a new menu object,
JSONObject menuObject = jObject.getJSONObject("menu"); Now lets extract its atrtibutes ,
String attributeId = menuObject.getString("id"); String attributeValue = menuObject.getString("value"); JSONObject popupObject = menuObject.getJSONObject("popup");
since “popup” is not plainly a String lets extract it to an object ,
3> Now popup contains an array of “menuitem”
So, we’ll have to extract it to a JSONArray,
JSONArray menuitemArray = popupObject.getJSONArray("menuitem"); Since it contains 3 items lets put it in a for loop.
for (int i = 0; i < 3; i++) { System.out.println(menuitemArray.getJSONObject(i) .getString("value").toString()); System.out.println(menuitemArray.getJSONObject(i).getString( "onclick").toString()); }
Basically thats it , u should see the output in the DDMS
4> The full code is as below,
import org.json.JSONArray; import org.json.JSONObject; import android.app.Activity; import android.os.Bundle; public class JsonParser extends Activity { private JSONObject jObject; private String jString = "{\"menu\": {\"id\": \"file\", \"value\": \"File\", \"popup\": { \"menuitem\": [ {\"value\": \"New\", \"onclick\": \"CreateNewDoc()\"}, {\"value\": \"Open\", \"onclick\": \"OpenDoc()\"}, {\"value\": \"Close\", \"onclick\": \"CloseDoc()\"}]}}}"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); try { parse(); } catch (Exception e) { e.printStackTrace(); } } private void parse() throws Exception { jObject = new JSONObject(jString); JSONObject menuObject = jObject.getJSONObject("menu"); String attributeId = menuObject.getString("id"); System.out.println(attributeId); String attributeValue = menuObject.getString("value"); System.out.println(attributeValue); JSONObject popupObject = menuObject.getJSONObject("popup"); JSONArray menuitemArray = popupObject.getJSONArray("menuitem"); for (int i = 0; i < 3; i++) { System.out.println(menuitemArray.getJSONObject(i) .getString("value").toString()); System.out.println(menuitemArray.getJSONObject(i).getString( "onclick").toString()); } } }
No comments:
Post a Comment