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.
Related
I am trying to build an android app that uses the github api.
I am facing an issue with JSON parsing.
I have a function that looks for JSONArray and produces the corresponding JSON data to show them in the UI, but the problem is the function works only when the JSON root is an array.
For ex-
when the url is "https://api.github.com/users", it works perfect, since the root is an array but now when I go to url such as "https://api.github.com/users/mojombo", the JSON root becomes an object. How do I parse it now in order to show the data in the UI ??
Do I have to write separate function for JSONObjects??
**The java function is **
private void makeJSON(String res) throws JSONException {
JSONArray root = new JSONArray(res);
for (int i =0; i<root.length();i++){
JSONObject jsonObject= root.getJSONObject(i);
String name = jsonObject.getString("login");
int id = jsonObject.getInt("id");
}
}
The answer is yes. Here you try to parse different types of objects (users list and particular user info) in single function. This is violation of single responsibility principle.
You can divide this function in 2 (to parse users list and user data) but better is to use Retrofit 2 for this.
I have two json arrays with different contained objects and i need to get serialized one array of objects that contains object from second array.
For example i have json response.
{
"shops":[{"id":"id"}],
"notifications":[{"shop_id":"id"}]
}
In result i need to have a list of Notification that will contain references to objects in the shops list. And i want make this in parse/deserialization step
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.
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?
I am writing an android app and need to copy the contents of 1 json object to 4 seperate json objects (depending on which values are stored in the object). how do i do this?
you have available two different constructor for this purpose take a look to the JSonObject documentation at:
JsonObject(JSonObject, String[])
and
JSonObject(String)