Hi need to call JSONArray
{"questions":{"poll_id":"1","user_id":"110","questions":"Captial of USA?"},"answers":[{"poll_id":"1","answer_id":"1","answer":"New York"},{"poll_id":"1","answer_id":"2","answer":"New Jersy"}]} in to the listview, im new to the android plz.... help me in this.
I got questions part, but i need to show answers part in the listview.
Its really simple JSON response.
To parse JSON response, there are 2 classes mainly useful: JSONObject, JSONArray and use methods of these classes: getJSONObject(), getJSONArray(), and getString().
Related
I have a problem. I am parsing a JSON file from a website, and want to go through it using a for loop and display the content each JSON object contains. Each JSON object should correspond to one for loop cycle, and it should contain a header where a title and some information get displayed, and a ListView with more information about the JSON object. How would I do that the best way ?
The amount of JSON object is unknown and changes, so I assume all the views have to be created programmatically using a for loop.
It should look somewhat like this.
I'm not going to code that for you, but basically you could do the following:
Download JSON data with an AsyncTask
Parse JSON (you could GSON or Jackson) into a List<>
Use an ArrayAdapter or crate your own list adapter
Add the adapter to a ListView
Done
I have json with some objects in it. And only one object from json i need. So which approach will be better for speed and performance - parse only this object or parse whole document instead?
If you want to show only the particular JSON Object which may be either inside a JSON Object or JSON array itself then you just parse that particular JSON Object instead of parsing whole document.
If you are not going to use rest of the data, you can just parse the item you only need.
In theory, just the object would get you a result faster with lesser work times. So just parse the object you need.
I was just wondering if anyone could recommend a better alternative method than org.json for decoding a complex JSON string. For reference, this will be coming from a web server down to Android (& iOS, but that's the other dev's problem!) devices, it doesn't have to go back up.
The string is of the following nature...
{"header":"value","count":value, ..., "messages":[
{"messagetype":1,"name":"value"},
{"messagetype":2,"name":"value","name":value},
{"messagetype":1,"name":"value"},
{"messagetype":3,"name":"value","subvalues":["value",value,value]},
...
{"messagetype":4,"name":value,"name":"value","name":value}
]}
Basically, there are some header fields which I can always rely on but then there will be an "array" of messages, variable in count, content and order.
I've been researching this for a few days now and have dismissed GSON and a few others because that either need to know the exact structure in advance and/or don't deal well with the embedded types (the contained messages).
Answer three in this question pointed me to using the org.json library and I know I can use that to parse through the string but I guess one of that answer's replies ("That's super old school and nobody uses that library anymore in the real world") has made me question my approach.
Can anyone suggest a library/approach which would handle this problem better? If anyone else has used an alternative approach to dealing with this type of complex and variable structure, I'd really appreciate your input.
Thanks in advance.
I really do not agree with the opinion about org.json libray: "That's super old school and nobody uses that library anymore in the real world", since parsing json by using this library is pretty straightforward. Besides, how complex can json get?, I mean, is all about key/value pairs, nothing that can't be solved with a few lines of code, for instance I will illustrate you a few cases, so that you'll get convinced that is pretty simple to do:
Suppose you have a response from the server containing all info you need formatted in a json array, then you can do something like this to parse the String:
JsonArray arrayJson = new JsonArray(response);
But now you want to access arrayJson childs:
for (int i = 0; i < arrayJson.length() - 1; i++)
{
JsonObject json = arrayJson.getJSONObject(i);
}
And now assume you have another array of json's inside those you retrieved in the for loop:
Then you'll get them this way:
for (int i = 0; i < arrayJson.length() - 1; i++)
{
JsonObject json = arrayJson.getJSONObject(i);
JSONArray anotherArray = json.getJSONArray("key");
}
....., more nestings you can handle them the same way, so I think I established my point. Remember that sometimes, struggling on finding easier ways to do things, can get them even harder to do.
i am newbie to android, I wanted to implement dynamically JSON data to my android listView, I have only to objects in JSON file this are dealname and discount.
I checked all questions and tutorials but when i implemented those codes in application, neither one is running. I just wanted to ask, to implement such JSON data, should i need to parse data and convert into String array so simple listview work, or i need to implement custom listview?
Plz your suggestions will help to solve this assignment..
Thanks in advance.
Look at these two tutorials, In these Json object are displayed in Custom ListView, I think this is what you needed,
Android Putting Custom Objects in ListView
Populate Listview from JSON
For simple example no need of custom list view. you can work with simple list view.
Parse the JSON data retrieve the data
store them in a list and pass that list as an argument to the adapter
set the adapter to the list view.
You will need custom list adapter, and possibly custom list entry.
Assuming you have already parsed your JSON and have JSON Array,
this adapter will have to:
- provide amoubnt of entries in JSON arary
- create and populate entry views on demand for certain list entry identified by position. Keep in mind, that list entries are reusable ( and they should be reused to reduce amount of object alloaction)
If there is a lot of data in JSON, better approach would be to use GSON pull parser and some databinding , to create java objects istead of built in JSON parser
I have been searching for a way to get the json data from a URl (for example: http://search.twitter.com/trends.json) and display it in a listview. Couldnt get a perfect example to get it done. Can anyone plz help me out by getting the solution and providing a good example of how to do it...
found this tutorial : http://damonsk.com/2010/01/jsonarray-httpclient-android/
Android comes with Json-lib so parsing JSON is easy. You just need to use the HTTPclient classes to retrieve the data from the URL, and you can use the JSONObject class to parse the JSON. And write a ListAdapter to pull data from the JSONObject.