I have an object like this one:
"choice": {
"000": {
"id": "001",
"label": "test",
"status": "0"
},
"001": {
"id": "001",
"label": "test",
"status": "0"
},
"002": {
"id": "001",
"label": "test",
"status": "0"
},
"003": {
"id": "001",
"label": "test",
"status": "0"
},
"004": {
"id": "001",
"label": "test",
"status": "0"
}
},
How can I parse that object with Gson+Retrofit? Or generate a POJO? There is an easy way of doing this?
Many thanks!
The main idea that all that you have in choice json object is Map:
public class RootObject{
Map <String,ChoiceEntry> choice;
}
public class ChoiceEntry{
String id;
String label;
int status;
}
You can create a POJO by pasting that JSON code into this link: http://pojo.sodhanalibrary.com/.
You posted a snippet that isn't properly formatted. I believe you'll have a multi-class POJO, and that is tricky to work with for certain uses such as listviews.
Let me know how it goes. Retrofit's really nice to use, but extremely annoying to figure out!
There's an addon for intellij/android studio for easy POJO generation.
Disclaimer: I have no affiliation with the project.
Related
I am a newbie to android. I am stuck at JSON parsing.
Following is my JSON, in this, the type of menu categories will be dynamic, and I want to arrange this JSON in sectioned recycler view.
Please help me out.
This is my JSON
{
"menu_items": {
"Plat": [{
"menu_name": "jambon à l'os",
"menu_cost": "18.00",
"menu_count": "1",
"category": "Plat",
"menu_tax": "5.50",
"code": "€"
},
{
"menu_name": "tacos",
"menu_cost": "6.00",
"menu_count": "1",
"category": "Plat",
"menu_tax": "5.50",
"code": "€"
}
],
"Entrée": [{
"menu_name": "avocat",
"menu_cost": "7.50",
"menu_count": "1",
"category": "Entrée",
"menu_tax": "5.50",
"code": "€"
}]
},
"order_data": {
"order_id": "278",
"order_number": "REF-5d5e6b86",
"created_at": "1566468998",
"instructions": "",
"fullname": "Rohan Chitnis",
"phonenumber": "7610771871",
"useraddress": "Brest, France",
"image_path": "https://restau-at-home.bzh/uploads/attachments/Jellyfish_2019_08_21_10_49_21.jpg",
"wp": "pune",
"code": "€",
"promocode": null,
"promo_value": null,
"promo_type": null
},
"order_details": {
"total_items": "3",
"order_total": "31.50"
},
"promo_applied": "0",
"error": {
"code": "13",
"status": "200",
"message": "Ok"
}
}
Check this out. Create a generic response class. Extend your classes from that class. To do this properly you can look at Java Generics and Inheritance topics if you are using Java. And then search about Sectioned RecyclerView in Android. Here is a library for this.
I have started using retrofit:2.0.2 first time into my new project and I have done with first service call using retrofit but am not sure is it correct way or not here is what i have done
web services response
{
"status": true,
"message": "",
"data": {
"schools": [ { "id": "1", "name": "test 1" }, { "id": "2", "name": "test 12" }],
"referrals": [ { "id": "195", "name": "test 1" }, { "id": "1483", "name": "test 12" }],
"Brands": [ { "id": "195", "name": "test 1" }, { "id": "1483", "name": "test 12" }],
"Teams": [ { "id": "195", "name": "test 1" }, { "id": "1483", "name": "test 12" }],
"positions": [ { "id": "195", "name": "test 1" }, { "id": "1483", "name": "test 12" }],
}
}
created 3 model classes to map above response
public class SimpleObject {
int id;
String name;
// getter setter
}
public class SimpleData {
private List<SimpleObject> schools = new ArrayList<SimpleObject>();
private List<SimpleObject> referrals = new ArrayList<SimpleObject>();
private List<SimpleObject> positions = new ArrayList<SimpleObject>();
private List<SimpleObject> Teams = new ArrayList<SimpleObject>();
private List<SimpleObject> Brands = new ArrayList<SimpleObject>();
// getter, setter
}
public class ResponseData{
boolean status;
String message;
SimpleData data;
// getter setter
}
and then made a service call using Retrofit2
call.enqueue( new Callback<ResponseData>() {
#Override
public void onResponse(Call<ResponseData> call, Response<ResponseData> response) {
}
#Override
public void onFailure(Call<ResponseData> call, Throwable t) {
}
and its working fine but a want to insure that is it best way of doing this or can any one suggest the best way of handling such a response without creating multiple model classes for simple data (there should me only one model class "SimpleObject" and other will be list of "SimpleObject")
Please comment or suggest best way to handling response thanks.
The way you currently have it structured looks correct. You need to have one model for every nested level in the JSON response, which you have.
The only other option is simply breaking the object up using keys to pull all of the JSONObjects and JSONArrays, which I do not recommend.
copy and paste your json response in the link given below and download zip file and put it in your project directly.
This link will convert your any kind of json response and convert it into POJO
I wanna create some Objects in my Realm using "realm.createOrUpdateAllFromJson(type.class, json-array)". But it throws error -> could not map json.
Heres the Json-Array. It's valid.
[
{
"uuid": "99975b79-adac-4baa-a1ab-96c0db23c0ee",
"name": "Joris"
},
{
"uuid": "0",
"name": "Adrian"
},
{
"uuid": "0",
"name": "Oliver"
},
{
"uuid": "0",
"name": "Michael"
},
{
"uuid": "0",
"name": "David"
},
{
"uuid": "0",
"name": "Daniel"
}
]
and heres my Owner-Model.
public class Owner extends RealmObject{
#PrimaryKey
private String uuid; //PK
private String name;
private String token;
private RealmList<Idea> ideas;
private RealmList<Vote> votes;
Here's the code where the error is thrown. its in the 3. line
realm.beginTransaction();
realm.createOrUpdateAllFromJson(Tag.class, tagJson);
realm.createOrUpdateAllFromJson(Owner.class, ownerJson);
realm.commitTransaction();
it's strange how the creating of Tag-Objects works fine..
please help!!
greetings john
You UUID is marked as a #PrimaryKey and you have multiple "uuid"'s with the value 0. This means that you are breaking the primary key constraint of only having one element with each uuid. If you look closely in LogCat you will probably see that Could not map JSON exception mentions this as well.
Have been scratching my head on this one. The JSON Response is a valid one:
{
"MRData": {
"xmlns": "http://ergast.com/mrd/1.4",
"series": "f1",
"url": "http://ergast.com/api/f1/current/2.json",
"limit": "30",
"offset": "0",
"total": "1",
"RaceTable": {
"season": "2014",
"round": "2",
"Races": [
{
"season": "2014",
"round": "2",
"url": "https://en.wikipedia.org/wiki/2014_Malaysian_Grand_Prix",
"raceName": "Malaysian Grand Prix",
"Circuit": {
"circuitId": "sepang",
"url": "http://en.wikipedia.org/wiki/Sepang_International_Circuit",
"circuitName": "Sepang International Circuit",
"Location": {
"lat": "2.76083",
"long": "101.738",
"locality": "Kuala Lumpur",
"country": "Malaysia"
}
},
"date": "2014-03-30",
"time": "08:00:00Z"
}
]
}
}
}
The POJO for the response:
public class ApiResponse {
MRData mrdata;
public class MRData {
String xmlns;
String series;
String url;
String limit;
String offset;
String total;
RaceTable raceTable;
}
}
The apiResponse object is always null. Anyone can point out what is wrong with the POJO object here?
Thanks.
I see a couple of potential issues here:
You are defining a class inside another class; I have never seen it done like this. You might want to separate in two different files.
Your variable names on your POJO should match the variable names on the JSON response exactly. For example:
public class ApiResponse {MRData MRData;}
If you want your POJO's variables to be different than what the JSON sends back, you should use #SerlizedName.
How can I easily store this large amount of data into A SQLite database?
I read that I could use Gson to parse this data, but I'm not exactly sure how to do that.
I already have created a SQLiteHelper and the necessary classes, I am just stuck on how to parse this data.
I am getting a httpResponse that returns a large amount of Json that looks like this:
"objs": {
"ptoStatus": [
{
"id": 1,
"modifieddate": "2006-07-06 05:35:38",
"description": "Submitted",
"name": "Submitted",
"createddate": "2007-07-06 09:43:38"
},
{
"id": 2,
"modifieddate": "2006-07-06 09:35:38",
"description": "Approved",
"name": "Approved",
"createddate": "2009-07-06 09:35:38"
},
{
"id": 3,
"modifieddate": "2009-07-06 09:50:38",
"description": "Denied",
"name": "Denied",
"createddate": "2009-07-06 09:35:38"
}
],
"alertStates": [
{
"id": 1,
"createddate": "2008-02-11 09:11:57",
"modifieddate": "2008-02-11 09:11:57",
"description": "Sending"
},
{
"id": 2,
"createddate": "2008-02-11 09:11:57",
"modifieddate": "2008-02-11 09:11:57",
"description": "Response"
},
{
"id": 3,
"createddate": "2008-02-11 09:11:57",
"modifieddate": "2008-02-11 09:11:57",
"description": "Acknowledge"
},
//etc....
Retrofit is a great REST client for android applications. Here you can find the documentation:
http://square.github.io/retrofit/
You can use it for getting the http responses, but with it you can just turn all your response data into Java objects (by default it uses Googles Gson parser for that)
For that particular json you need to create something like this:
public class ObjsResponse {
public MyObjects data;
public class MyObjects {
List<States> ptoStatus;
List<AlertStates> alertStates;
}
}
And the models you store in the database just need to have the same names (or you can use annotations for different keys in the json, but that is all documented at Gson), like this:
public class States {
int id;
String modifieddate; //here you can use some sort of DateTime too (for example jodatime)
String description;
String name;
String createddate;
}
And here you can use the database annotations or anything you like.
After this you just have to set this ObjsResponse class as the return param to the retrofit interface (described at the upper link), have it initialized, then you can call this method. I don't write down the hole thing, in the documentation's Introduction section is basically all you need ;)