In my application I want get some data from server and show this into list (recyclerview)!
I receive data from server such as below :
"data": {
"id": 1812286134,
"Cr": "85",
"BUN": "87",
"ALP": "75",
"ALT": "6",
"AST": "6",
"Chol": "55",
"HDL": "545",
"LDL": "45",
"TG": "4",
"created_at": "2022-10-30 16:05:50",
"updated_at": "2022-10-30 16:05:50"
}
UPDATE :
This data may update in some time, added new items or remove items by admins
I should show all of this items into recyclerview.
I know in above json data should list but has object.
But backend developer tells me convert all of this data to list with key value!
My question is can I convert this data to list and use all of items into list ?
How can I it?
Try to use Map<String,String> in your Response or whatever you used.
It will be something like this.
#GET
fun getCurrency():Response<DataResponse>
data class DataResponse(
val currencies:Map<String,String>
)
Related
I would like to use One recycler view and One adapter with date header type
And I am having below JSON response from backend server,
if there is any better way to use different JSON format please suggest.
kindly refer attached image
{"data": [
{
"name": "ddmmyy",
"data": [{"title": "ABC"}, {"title": "XYZ"}]
},
{
"name": "ddmmyy",
"data": [{"title": "XYZ"}, {"title": "XYZ"}]
}
]}
friends:
I get a response from remote server as follows on Android:
{
"items": {
"persons": [
{
"id": "200120",
"name": "Bill"
},
{
"id": "200121",
"name": "Jim"
}
],
"tasks": [
{
"id": "001",
"name": "Fetch ten books",
"deadline": "5:30 p.m.",
"scores": "10"
},
{
"id": "002",
"name": "Fetch thirty books",
"deadline": "5:30 p.m.",
"scores": "30"
}
],
"intro": "This is a funny game."
},
"otherObj": []
}
And I want to save it to phones. I do not think it a good choice to save it into databases. However it read not fast if put the response to SharedPreferences file. Is there any other way?
No need to save this data into database as these value are dynamic and will change time to time.
In your Activity, call the API and get JSON as response and then parse this JSON and show it on device.
To store it on device u can use Singleton class also.
For JSON parser refer to http://www.tutorialspoint.com/android/android_json_parser.htm
You can first save your data into objects and at last into Object Array and then Save to database.(If your data is dynamic it is better you only deal with Object not database)
For saving to database you can use
db.beginTransaction();
for(object : ObjectArray){
db.dboperation();
}
db.setTransactionSuccessful();
db.endTransaction();
I have this:
String foo =
"options": {
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
}
But the JSONObject constructor rearranges the keys:
new JSONObject(foo).toString() =
"options": {
"6",
"3",
"7",
"2",
"5",
"1",
"4",
"8",
"9",
}
The new order is reproducible, meaning each execution results in the same (seemingly random) rearrangement.
That does not look like valid JSON to me. First, there is not {} or [] surrounding the entire string. Second, it looks like the data associated with options should be inside and array ([]) not be an object({}). If it was an array, the order would be preserved, whereas with the values just being declared as properties, there is no guarantee on their order.
In json sequence of the fields is not important, only their nesting.
Your example Strings are invalid JSON. You have a bare top-level field options, and no values for it's fields.
A javascript object, it's JSON representation, and in particular a JSONObject, do not guarantee an ordering of the fields.
From the JSONObject javadoc:
A JSONObject is an unordered collection of name/value pairs.
You can maintain an ordering by using an array. So if your data is or can be represented as an array - from your example it looks like an array - then the order of the elements will be maintained.
Hi I have following JSON
{
"code": 0,
"response": {
"userObject": {
"User": {
"id": "355660",
"first_name": "Dummy",
"last_name": "dummy",
"email": "dumb#email.com",
"birthday": "2012-05-07",
"created": "2012-08-21 06:41:05",
"modified": "2012-08-21 06:41:05",
"image_url": null,
},
"Location": {
"id": "273550",
"name": "New York City",
"asciiName": "New York City",
"lat": "40.714272",
"lon": "-74.005966",
"geoname_modified": "2011-11-08 00:00:00",
"timeZone": "America/New_York",
"countryName": "United States",
"state": "New York",
"created": "2012-07-12 12:11:01",
"modified": "2012-08-20 14:27:24"
}
}
}
}
I have two classes, one each for Location and User
I know that I can get the objects if I create nested class like
response
->UserObject
*User
*Location
But i don't want to create two extra classes for UserObject and response just for wrapping the two POJO's .
Is there any simpler way to do it??
I am using Jackson Parser with Spring for android
You can also do it in two steps, if you really want to avoid throw-away classes, like:
JsonNode tree = mapper.readTree(...);
User user = mapper.treeToValue(tree.path("response").path("userObject").get("User"), User.class);
Location loc = mapper.convertValue(tree.path("response").path("userObject").get("Location"), Location.class);
but yeah I might go with silly struct-classes instead:
static class Response {
public UserObject userObject;
}
static class UserObject {
public Location Location;
public User User;
}
since it really isn't much more code.
Rather than creating classes you could create arrays or use hashmap. Personally, I would just create the classes. I think that this give you more flexibility in your app, and will allow you to work with the objects with less hassle. I know it takes time to set them up, but once you do that, you can use ArrayList and you can parse the JSON quite a bit easier.
I am having a few problems getting Facebook data to parse properly.
I am working on implementing part of an application to allow a user to select and use one of their own facebook photos within the app. I have gotten the facebook login/logout code working and I am currently getting the token (once logged in) in order to gather the users Album information. The permission set is also working nicely, however I am now stuck at trying to get the JSON information to parse correctly. Here is a sample snippet of the information I need to parse:
"data": [
{
"id": "3486732467234",
"from": {
"name": "Persons Name",
"id": "Persons ID"
},
"name": "Vacation",
"location": "City",
"link": "https://www.facebook.com/album.php?fbid=434235&id=324343&aid=2430",
"cover_photo": "3489234432",
"privacy": "everyone",
"count": 60,
"type": "normal",
"created_time": "2007-06-03T23:01:16+0000",
"updated_time": "2011-03-18T19:46:43+0000",
"can_upload": true
},
{
"id": "4043544665",
"from": {
"name": "Persons Name",
"id": "Persons ID"
},
"name": "Vacation 2",
"location": "City",
"link": "https://www.facebook.com/album.php?fbid=4043434665&id=508154335&aid=2555",
"cover_photo": "5434543",
"privacy": "everyone",
"count": 60,
"type": "normal",
"created_time": "2007-06-03T22:53:03+0000",
"updated_time": "2011-03-18T19:45:55+0000",
"can_upload": true
}],
...more paging JSON information ...}}
I need to be able to pull the album information in order to display the photo album names in a list (and use the ID in another query). Once I can get the albums to show, I would then use those IDs to perform another query to get the photos from that album. Again I can also get that information back, I am just not sure how to properly parse it.
Any useful tips that someone might have in being able to parse this into a listview would be greatly appreciated.
I need to target pulling the first ID and the Album name ("id": "3486732467234" and "name": "Vacation" from the first listing)
why don't you try with the json classes...
something like this:
jsonObject = new JSONObject(your_data);
jArray = jsonObject.getJSONArray("data");
for(int i =0;i<jArray.length();i++){
String name= jArray.getJSONObject(i).getString("name");
String location= jArray.getJSONObject(i).getString("location");
etc.
The new Android Facebook SDK 3.0 returns a Response object in the Response.Callback() listener when making Graph API calls. This response object can be used to create a GraphObject which can be used to get a JSON...ex:
Request graphRequest = Request.newGraphPathRequest(session, graphRequestString, new Request.Callback() {
#Override
public void onCompleted(Response response) {
//Create the GraphObject from the response
GraphObject responseGraphObject = response.getGraphObject();
//Create the JSON object
JSONObject json = responseGraphObject.getInnerJSONObject();
}
});