What is JSONArray,JSONObject,JSONStringer and JSONTokenizer - android

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

Related

Json_ The value of the key depend on the value of other key

I get a server response like this:
{
"action":"add",
"domain":"dm1",
"params":{
"add1":"v1",
"add2":"v2",
"add3":"v3"
}
}
And i have many domain (15) and each domain have multi action, and key in value of "params" key ("add1", "add2",...) that are depended on value of action value ("add", "remove", ...). So, i want to ask that, how can i get param value from server then push in a Model using Gson. I'm Android.
Thanks in advance!
You can parse your params key as a JSONObject and iterate through all its keys to get the values of those dynamic keys. Use JSONObject.keys to get an Iterator for keys. This answer should further help you - How to parse a dynamic JSON key in a Nested JSON result?
I am not pretty sure. What your goal is.
But You can do as below
{
"action":"add",
"domain":"dm1",
"params":[
{ "add":"v1" },
{ "add":"v2" },
{ "add":"v3" }
]
}

How to display a JSON file in a TextView

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.

Parsing JSON with identical keys in object

One of the responses from the New York Times API come in the form of JSON like this.
"multimedia":" ",
"multimedia":[ { array of objects } ]
These two name-value pairs are in the same JSON object. I am using Retrofit to take this response into a POJO object. As you can imagine I am having problems doing that since there are two values with the same name. How do I obtain the multimedia that I want?

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.

Categories

Resources