JSON is Javascript Object Notation. Basically it is a part of Javascript programming language. Now a days it is generally use for data exchange between server and client. It is light weight data exchange format and takes lesser memory than xml. Data is stored in JSON as key-value pair. Basic Format of JSON :
{
"key1":"value1",
"key2":"value2",
"key3":"value3",
....
....
....
....
}
JSON in Android
Android provide two classes which is used to implement JSON functionality in Android .
JSONObject
JSONArray
1) JSONObject : JSONObject is used to convert JSON String to JSON Object and vice a versa. We can also create own JSON object from our raw data. For Example -
JSONObject jsonObj = new JSONObject();
jsonObj.put("name","Sam");
jsonObj.put("age","23");
jsonObj.put("address","USA");
console.log("JSON String",jsonObj.toString());
Result on console :
{"name":"Sam","age":"23","address":"USA"}
You can also convert JSON string to JSON object by passing json string into the constructor of JSONObject class.
String jsonString = "{'name':'Sam','age':'23','address':'USA'}";
JSONObject jsonObj = new JSONObject(jsonString);
To print key's value you need to call getString('key') method of JSONObject class.
No comments:
Post a Comment