First of all try to understand AsyncTask also. It allows the user to perform a long background
operations and to show the results back to the user in the MainUI Thread. You can perform JSON parsing and get values which is stored inside the JSON file
via AsyncTask.
JSON is very light, easy to understand and it's the best alternative to XML
. To parse your JSON file you need to know about JSONArray
and JSONObject
. In a JSON file , square bracket [
represents a JSON array and curly braces {
represent the JSON objects. JSON is structured with Key and Value pairs
. Get your string values with getString("key");
and then just display it into TextView
JSONArray - It contains many JSON Objects
.
JSONObjects - It contains key
and value
pairs.
- [ ->
It represents the JSON Array
- { ->
It represents the JSON Object
Two methods getJSONArray()
and getJSONObject()
are mainly used in the JSON to represent the json node
.
Get your JSON Array
node.
JSONArray booksArray = jsonObj.getJSONArray("books");
JSON Code :
{
"books": [ //JSON array
{ //represents JSON Object
"id":"440", //Key and Value pair
"edition": "Fourth",
"language": "Java",
},
{
"id":"407",
"edition": "second",
"language": "Python",
}
]
}
Above code has one JSON Object(books) and one JSON Array it holds two json objects. See this for JSON Parsing
in Android.
You can also upload
your JSON file for free pastebin and view your JSON file as tree structure with JsonViewer
NOTICE : Comments
are not officially supported in JSON. So I just added comments like in JAVA.