How to display a JSON file in a TextView - android

I'm very new to Android and having a lot of trouble understanding JSON. I have a separate JSON file with my values written out but no idea how to actually display those strings in an Android app.
Any comprehensive tutorials or basic-level explanations are very welcome. Thanks!

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.

Related

Android Volley Post JSON Array with paramater and getting response in JSON Object

I am facing strange problem with Google Volley I hope some one help me out quickly. I want to send JSON array in parameter and server will give me response in JSON object. How can we achieve this?
e.g
I wanted to post this JSON array.
[
{
"name":"John",
"age":"30",
"cars":"6"
},
{
"name":"John",
"age":"30",
"car
}
]
and server will send response in JSON object format like this.
{
"status":"success",
"code":30,
}
Can someone explain to me how I can achieve this thing?. Moreover, my JSONarray consist of mobile contacts and size will be large.
You can use Map<Object, Object> where value be collection of json Element you want. You can make custom deserilizator with lib like "gson".
You should try: JSONArray arr = new JSONArray(JSON_STRING);

What is JSONArray,JSONObject,JSONStringer and JSONTokenizer

I'm new to Android. I have learnt some basic concepts in Android. Now I'm learning JSON, I wanted to know the definitions of JSONArray,JSONObject,JSONStringer and JSONTokenizer. I'm a bit confused with these terms.Can anyone provide me the correct definition for these terms??
Thanks
json array:
[
{
"id":711
}, {
"id":712
}
]
json object:
{
"id":711
}
1) Array([)
In a JSON file , square bracket ([) represents a JSON array.
2) Objects({)
In a JSON file, curly bracket ({) represents a JSON object.
3) Key
A JSON object contains a key that is just a string. Pairs of key/value make up a JSON object.
4) Value
Each key has a value that could be string , integer or double e.t.c
see more detailed explanation here:http://www.tutorialspoint.com/android/android_json_parser.htm

Parsing JSON data from an XML web service in Android

I have an XML web service. I want to parse this XML and I want to store in an separate textviews. The following is an XML content, and I have finished getting it in a String variable.
{
"30_year_rate": 4.25,
"30_year_pi": 196.78,
"30_year_apr": 4.375,
"20_year_rate": 4.375,
"20_year_pi": 250.37,
"20_year_apr": 4.5,
"15_year_rate": 3.75,
"15_year_pi": 290.89,
"15_year_apr": 3.875,
"10_year_rate": 3.625,
"10_year_pi": 397.89,
"10_year_apr": 3.75,
"5_year_arm": 2.75,
"5_year_pi": 163.3,
"5_year_apr": 2.875,
"7_year_arm": 3,
"7_year_pi": 168.64,
"7_year_apr": 3.125,
"3_year_arm": 4,
"3_year_pi": 190.97,
"3_year_apr": 4.125,
"5_year_io_arm": "N/A",
"5_year_io_pi": "N/A",
"5_year_io_apr": "N/A",
"QuoteID": 1449,
"Licensed": "N"
}
How can I parse this data? I want to convert it to a JSON object and retrieve it, or any other reasonable approach.
If what you're getting back from the webservice is the string above, then you already have a JSON string. To create an object that can retrieve information from it, use something like JSONObject.
JSONObject object = new JSONObject(your_string_variable);
double thirtyYearRate = object.getDouble("30_year_rate");
String licensed = object.getString("Licensed");
etc.
You might (will) run into some issues where you try to pull a double from a JSON field that contains a string; i.e., the "N/A" fields above. You'll likely have to pull them out as strings and then try to parse doubles from them, and if the parsing throws an exception, you'll know it's a string.
Alternately, you could look into JSON binding with something like Jackson, which apparently runs on Android.
To parse JSON, you could use the built in JSONObject org.json Or Json-lib if you use an old version of android.
To parse XML, use XMLPullParser. A sample can be found here: Parsing XML Data

multiple json string in android

I have to return all my JSON string. For example I have one json string:
[{"Locationvalue":"Payroll - 9","LocationId":"465","IsSelected":false}]
and also returned second JSON string:
[{"CC2Description":"Denver - DN","CC2":"DN","isSelected":false},{"CC2Description":"Las Vegas - LV","CC2":"LV","isSelected":false}]
ans so on.
In android I have written this:
JSONArray JsonObject = new JSONArray(JsonString.toString());
for(int i=0;i<JsonObject.length();i++)
{
Log.v("log", JsonObject.getString(i));
}
but I can only access one JSON array. I want other JSON array also.
You cannot decode multiple separate json structures in a single call. A JSON structure must be a complete proper Javascript object or array on its own, e.g.
Two arrays like this:
[1,2,3][4,5,6]
is invalid, because it's two separate arrays smashed up against each other. However,
[[1,2,3],[4,5,6]]
is ok, because it's a single array that contains two separate child arrays. You can return multiple separate json strings, but they must be contained within a single structure.

json parsing in android of web data

I have to parse this json data. The data begins with [ and ends with ]
How can we parse such json data? json data usually starts with {..[..]..}
Just create a JSONArray from your input. There is even a constructor taking a String as parameter. So, basicly you need to do something like this:
String input = .. //read your input
JSONArray arr = new JSONArray(input);
//work with the array as usual..
take result data as a JSONArray and not a JSONObject.
It depends on how you parse the data. Built in json or google json(gson) etc.
But normally you dont have to care about that it starts with square bracket.
Show me what the json array/object look like and I can give you an example.

Categories

Resources