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?
Related
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.
I'm using com.fasterxml.jackson.databind with Retrofit to handle the response from the server in my Android app.
Since the JSONObject response is too complicated and contains lots of JSONArray, I want to be able to parse some of those array fields into String instead of creating POJO for each sub-object that those Array could contains.
Is there a way I could just tell Jackson to keep those field as String and not parse them into Entities?
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
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.
I have a question that I am a little bit confused about. I am quite new to JSON and getting JSON values in the android API. I am trying to access an array within the response I get. the JSON code I am getting is something like this:
Response:
{
"event": {
"participants": []
},
"status": "success"
}
How would I access the participants array and store their values. This is what I am trying at the moment... but I dont appear to be getting what I want.
try{
//get the JSON values from the URL.
JSONObject json = jParser.getJSONFromUrl("http://somesite.com/api/find?"+"somevar="+someJavaStringVar);
json_event = json.getJSONObject("event");
JSONArray json_array_participants = json_event.getJSONArray("participants");
} catch(JSONException e) {
}
The thing I am mostly confused about is... what is the arrays type equivalent to. Any advice or reasoning as to the correct way to get ahold of that variables value would be great... thanks guys.. :).
Think JSON is really just a key-value pairing. The JSONArray type is just an array full of objects (like Object[]) - it has no idea what the objects it contains are or what they're to be used for. Its up to you to assign meaning to the JSON stream based on what you know of the source. From what I see of your code, most of it looks fine, though I don't know what your jParser.getJSONFromURL() is doing. Typically, you would build the JSON from the response string like so:
String jsonString = getJSONFromUrl("http://somesite.com/api/find?"+"somevar="+someJavaStringVar);
JSONObject json = new JSONObject(jsonString)
JSONObject json_event = json.getJSONObject("event");
JSONArray json_array_participants = json_event.getJSONArray("participants");
You can iterate through the array like any other array to get subobjects or whatever:
for(int i=0; i < json_array_participants.getLength(); i++) {
JSONObject participant = json_array_participants.getJSONObject(i);
// Do stuff
}
As a side note - I WOULDN'T use GSON until you understand the underlying protocol, at least a little - because you never know when you might want to parse your JSON from a different language for some reason.
I would strongly recommend to use gson instead as your preferred parser since it will do all the job of serializing and deserializing for you except creating the domain objects.
This tutorial should get you going:
http://www.javacodegeeks.com/2011/01/android-json-parsing-gson-tutorial.html
This will depend on what the server is supposed to return. It could be an array of anything and if this is a public service, there should be a specification to go off of.
If you are in charge of the server portion as well, and you have a backing object, Google's GSON library is extremely easy to use. It will also keep type information straight.