WEB DEVELOPER SITE JSON Parsing
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

JSON Parsing Tutorial


JSON stands for JavaScriptObject Notation. It is structured, light weight, human readable and easy to parse. Its a best alternative to XML when our android app needs to interchange data from server. XML parsing is very complex as compare to JSON parsing.

JSON is shorter, quicker and easier way to interchange data from server. JSON is great success and most of the API available support JSON format.

Android Provide us four different classes to manipulate JSON data. These classes are JSONObject, JSONArray, JSONStringer and JSONTokenizer.

In Android, JSON consist of many components. Below we define some common components.

1. Array([):  In a JSON, square bracket ([) represents a JSONArray. JSONArray values may be any mix of JSONObjects, other JSONArrays, Strings, Booleans, Integers, Longs, Doubles, null or NULL. Values may not be NaNs, infinities, or of any type not listed here.

2. Objects({): In a JSON, curly bracket ({) represents a JSONObject. A JSONObject represents the data in the form of key and value pair. JSONObject values may be any mix of other JSONObjects, JSONArrays, Strings, Booleans, Integers, Longs, Doubles, null or NULL. Values may not be NaNs, infinities, or of any type not listed here.

3. key:  A JSONObject contains a key that is in string format. A pair of key and value creates a JSONObject.

4. Value: Each key has a value that could be primitive datatype(integer, float, String etc).

JSONObjet Parsing 1. get(String name): This method is used to get the value from JSONObject. It returns the value of object type. We pass the String type key and it returns the value of Object type if exists otherwise it throws JSONException.

2. getBoolean(String name):  This method is used to get the Boolean value from JSONObject. We pass the String type key and it returns the value of Boolean type if exists otherwise it throws JSONException.

3. getDouble(String name):  This method is used to get the double type value from JSONObject. We pass the String type key and it returns the value in double type if exists otherwise it throws JSONException.

4. getInt(String name):  This method is used to get the int type value from JSONObject. We pass the string type key and it returns the value in int type if exists otherwise it throws JSONException.

5. getJSONArray(String name): This method is used to get the JSONArray type value. We pass the String type key and it returns JSONArray if exists otherwise it throws JSONException.

6. getJSONObject(String name): This method is used to get the JSONObject type value. We pass the String type key and it returns the JSONObject value if exists otherwise it throws JSONException.

7. getLong(String name): This method is used to get the long type value from JSONObject. We pass the String type key and it returns the value in long type if exists otherwise it throws JSONException.

8. getString(String name): This method is used to get the String type value from JSONObject. We pass the String type key and it returns the value in String type if exists otherwise it throws JSONException.

9. length(): This method is used to get the number of name/value mappings in this object.

10. keys(): This method is used to get the iterator of String names in the Object.

11. opt(String name): This method is used to get the value from JSONObject. It returns the value of Object type. We pass the String type key and it returns the value of Object type if exists otherwise it returns null.

12. optBoolean(String name): This method is used to get the Boolean value from JSONObject. We pass the String type key and it returns the value of Boolean type if exists otherwise it returns false.

13. optDouble(String name): This method is used to get the double type value from JSONObject. We pass the String type key and it returns the value in double type if exists otherwise it returns NaN.

14. optInt(String name): This method is used to get the int type value from JSONObject. We pass the string type key and it returns the value in int type if exists otherwise it returns 0.

15. optJSONArray(String name): This method is used to get the JSONArray type value from JSONObject. We pass the String type key and it returns JSONArray if exists otherwise it returns null.

16. optJSONObject(String name): This method is used to get the other JSONObject type value from JSONObject. We pass the String type key and it returns the JSONObject value if exists otherwise it returns null.

17. optLong(String name): This method is used to get the long type value from JSONObject. We pass the String type key and it returns the value in long type if exists otherwise it returns 0.

18. optString(String name): This method is used to get the String type value from JSONObject. We pass the String type key and it returns the value in String type if exists otherwise it returns emptly() string.


Welookups