JSONObject to custom object conversion - android

I receive from a web service a list of json objects that may containing different keys. Eg one would have a title, a url and a list of picture urls, another one would have the same more a phone number... I need to keep these objects in memory but I'm not sure about the best way to do it.
1- I could keep the JSONObject as it is.
2- I could create an array of generic Item object with all the possible fields (most are common to all items) and for each instance set only the fields which have the corresponding keys in the json object, and leave the others null.
3- I could also create for each object a map with the keys contained by the item.
Any thoughts about the best method?
Thanks
Jul

I would look at Jackson or GSON for JSON object mapping.

Related

Remove a pointer to a parse user from an array

I have a parseobject which consists of many objects, notably an array of consisting of ParseUser pointers.
When an individual clicks a button, the array should remove a certain User.
I don't get how to do this,
I have tried:
mRideEdit.removeAll("Participant", (Collection) childuser);
Where mRideEdit is my ParseClass, Participant is the array consisting of ParseUsers, and childuser is the user I want to remove
Please help,
I've recently faced the same problem.
It originates from how the ParseObjects (including ParseUser) are saved on the server.
If you look at the console, you'll see it is actually an array of strings created from the JSONObject toString method.
The simple answer is - You're using it wrong.
There is no sense in saving objects in such a way (though it's very intuitive).
As you probably noticed - you don't receive them as objects with the getJSONArray method either and have to get them from their objectIds.
The best way to do this is using relations.
If you're set on avoiding relations, what I'd suggest is saving them as strings from the get go (objectId). That way add, addUnique and removeAll will work just fine.
Hope this helps.

Android place view in for loop

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

Parsing JSON performance

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.

"global" array with different data types - Android

I'm new in programming for Android so maybe my question will be very easy to solve but still. I'm trying to make an array of different data types :
I have to add there :
int number
String name
int number_2
int time
int total
And my question now is how to implement it in easiest way, and how to get data from it. In case that I have to get a different records for this variables and store it into list .
Also have a question about way how to keep all values which I handle inside of my array.
I have to keep it because in my program I have to go back to other activities go forward to another and again collect data and add it to my array.
What will be the best and easiest solution ?
Thanks in advance for help
You could create the Array as an Array of Objects. All other classes are derived from Object, so you'll be able to store all types of objects in your Array. However, you would have to check the type of an object you get from the Array, before you'd be able to safely interpret as an object of a specific class. Moreover, you would have to use Integer instead of int.
If all (or at least multiple) of your elements you are intending to store in the Array are belonging to one (physical) entity, you could create a custom Class that holds its own properties as class members, and fill your Array with a list of instances of this Class.
Moreover, if you plan to add elements to your Array, you should use a List instead, e.g. an ArrayList.
As for retaining your data, you would have to either store it in a database, or save it to a file. In either way, you will have to save it upon close of the Activity, and load it again once the Activity starts
To pass the data across activities you will need to pass them using objects you can store in an intent. Seems like the best way to handle that is to either create a PREFS file to store the data or to create an object that extends Parcelable like here:
https://stackoverflow.com/questions/18593619/android-parcelable-object-passing-to-another-activity
Parcelables are preferable assuming you need all the data in a single object, you do not want to "putExtra" a bunch of fields and you also want to be sure data can pass from one activity to another. Otherwise, a simple Util class that reads and writes to a PREFS file is the way to go:
android read/write user preferences
A database is always another option, but seems well outside the scope of your question.

JSON object to listView

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

Categories

Resources