I want to parse my JSONObject with gson. I write some code but doesn't work.
public class User {
#SerializedName("Email")
public static String Email;
#SerializedName("Id")
public static int Id;
#SerializedName("Picture")
public static String Picture=null;
#SerializedName("UserName")
public static String UserName;
}
In my class:
User result=new Gson().fromJson(response,User.class);
My JSONObject:
{
"Email": "",
"Id": 1,
"Picture": null,
"UserName": "User1"
}
Remove static modifier from all variables and try it again.
Note: Make all variable private and provide public getter/setter methods. learn more...
Related
I need to convert below JSON to Java classes
Here is the JSON
{
"browserName": "chrome",
"env": "test",
"envUrls1": {
"qatesta": {
"fName": "test",
"lName": "test",
"email": "3242342",
"password": "passTestaa"
},
"qatestb": {
"fName": "test",
"lName": "test",
"email": "3242342",
"password": "passTestaa"
},
"qatestc": {
"fName": "test",
"lName": "test",
"email": "3242342",
"password": "passTestaa"
}
}
}
Using above JSON body I able to creates below classes.
1.
public class JsonConfMainObj {
private String browserName;
private String env;
private EnvUrls1 envUrls1;
}
2.
public class EnvUrls1 {
private Qatesta qatesta;
private Qatestb qatestb;
private Qatestc qatestc;
}
3. In each above 'Qatesta', 'Qatestb' and 'Qatestc' Java Classes Contains the below-repeated values
public class Qatestx{
private String fname;
private String lname;
private String email;
private String password;
}
But I need to avoid repeating the above attributes in each class and move them to common Class and map to 'Qatesta', 'Qatestb', and 'Qatestc'.
like below
public class Qatesta{
private Common com;
}
public class Qatestb{
private Common com;
}
public class Qatestc{
private Common com;
}
public class Common{
private String fname;
private String lname;
private String email;
private String password;
}
Is there a way to do this?
You Can use only One class and give the different variable assigned for that class for eg in EnvUrls1 give class Qatest assigned 3 different values
1.
public class JsonConfMainObj {
private String browserName;
private String env;
private EnvUrls1 envUrls1;
}
2.
public class EnvUrls1 {
private Qatest qatesta;
private Qatest qatestb;
private Qatest qatestc;
}
public class Qatest{
private String fname;
private String lname;
private String email;
private String password;
}
I have the following JSON modelling groups with students:
"groups":[{
"id": "1",
"grade":"second",
"group": "A",
"students":[{
"id": "1",
"address": "rhonda#pugh.no",
"name": "Geoffrey Guthrie",
"phone": "475690",
"parent": "Ronald Zhang",
"diagnosis": "Whitehead",
},
...}],
...}]
and their respective classes are as follows (setters and getters not included):
public class Student extends RealmObject {
private String id;
private String name;
private String address;
private String phone;
private String parent;
private String diagnosis;
}
and:
public class Group extends RealmObject{
private String id;
private String grade;
private String group;
private RealmList<Student> students;
}
My question here is if the method createOrUpdateAllFromJson, as stated above, supports those nested arrays or should I do it manually getting the arrays inside and then call the same function to create the list of students for each group?
No you don't need to manually getting the arrays inside and then call the same function to create the list of students for each group. But you will not be able to use
createOrUpdateAllFromJson() for JOSNArray
or
createOrUpdateObjectFromJson() for JSONObject
Because createOrUpdate requires a primary key. But according to your Json structure there is no primary key for the root object. My code blocks will describe the scenario.
Your Json Structure-
{
"groups":[{
"id": "1",
"grade":"second",
"group": "A",
"students":[{
"id": "1",
"address": "rhonda#pugh.no",
"name": "Geoffrey Guthrie",
"phone": "475690",
"parent": "Ronald Zhang",
"diagnosis": "Whitehead"
}]
}]
}
According this Your Models should be as follows-
public class Students extends RealmObject {
private String id;
private String name;
private String address;
private String phone;
private String parent;
private String diagnosis;
}
and:
public class Groups extends RealmObject {
private String id;
private String grade;
private String group;
private RealmList<Students> students;
}
and:
public class BaseObject extends RealmObject {
private RealmList<Groups> groups;
}
And then your transaction will be like this-
realm.executeTransaction(new Realm.Transaction() {
#Override
public void execute(Realm realm) {
realm.createObjectFromJson(BaseObject.class, json);
}
});
You can add #PrimaryKey annotation for Groups and Students id to avoid duplicate entry if necessary. Hope this structure will reduce your hassle.
I've been trying retrofit for Android. The response has been null. If my understanding is correct, this might be because of a 400 response or an incorrect modelling of the response in my model class. The response that I am getting is as follows:
{"itemA":"data",
"itemB":"data",
"itemC":"data",
"ItemC":"",
"result_arr":[{"Val1":"A","Val2":"","id":"id","pr":"$0.00","sid":"a","cid":"a","price":"$0.00","cool_down":"0%","url":"","name":"Name"},
{"Val1":"A","Val2":"","id":"id","pr":"$0.00","sid":"a","cid":"a","price":"$0.00","cool_down":"0%","url":"","name":"Name"}]
,"statusCode":"200"}
The models that I have defined are as follows:
API result
public class APIResultModel {
#SerializedName("itemA")
public String itemA;
#SerializedName("itemB")
public String itemB;
#SerializedName("itemC")
public String itemC;
#SerializedName("itemD")
public string itemD;
#SerializedName("results_arr")
public List<ProductModel> results_arr;
#SerializedName("status_code")
public String statusCode;
}
Result Array Model:
public class ResultArrayModel {
public String val1;
public String val2;
public String id;
public String pr;
public String sid;
public String cid;
public String price;
public String cool_down;
public String url;
public String name;
}
How should a model for this response look like? And how is the model derived from the response values?
Looking at your code you seem to be using Gson.
In order for Gson to create your pojo, your model's serializedNames have to match the json response you're getting.
You'll have to change:
#SerializedName("status_code")
to:
#SerializedName("statusCode")
Make sure all your attributes are following this rule and you're good to go.
Given the JSON:
{
"itemA": "data",
"itemB": "data",
"itemC": "data",
"ItemD": "",
"result_arr": [
{
"Val1": "A",
"Val2": "",
"id": "id",
"pr": "$0.00",
"sid": "a",
"cid": "a",
"price": "$0.00",
"cool_down": "0%",
"url": "",
"name": "Name"
},
{
"Val1": "A",
"Val2": "",
"id": "id",
"pr": "$0.00",
"sid": "a",
"cid": "a",
"price": "$0.00",
"cool_down": "0%",
"url": "",
"name": "Name"
}
],
"statusCode": "200"
}
Your api result model may be:
public class APIResult {
public String itemA;
public String itemB;
public String itemC;
public String itemD;
#SerializedName("results_arr")
public List<Product> products;
public String statusCode;
}
And your product model may be:
public class Product {
#SerializedName("Val1")
public String val1;
#SerializedName("Val2")
public String val2;
public String id;
public String pr;
public String sid;
public String cid;
public String price;
#SerializedName("cool_down")
public String coolDown;
public String url;
public String name;
}
Supposing you are using GSON, you should only use the SerializedName annotation when the field name is not the same as the present in JSON.
There are some applications that do a conversion from JSON to POJO, Tyr for example.
I have a class that should be deserialized accordingly the header request.
If header is on V1 version, ww should output the information field of Product class, like a String. Otherwise it output an Info object.
Is there another solution to do this, instead duplicate the class?
public class Product{
private String name;
private Integer id;
private Info information;
}
public class Info{
private String generalInfo;
private String fullDescription;
private String code;
}
public class Product{
private String name;
private Integer id;
private String information;
}
Above the JSON when use INFO object and when information is a string.
{
"name": "Paul",
"id": "123123,
"information": {
"generalInfo":"Business Product",
"fullDescription":"23",
"code":"9487987289929222-3"
}
}
{
"name": "Paul",
"id": "123123,
"information": "Business Product - 23 - 9487987289929222-3 "
}
This is my JSON:
{
"results": [
{
"user_id": "1",
"item_id": "18630",
"name": "Unnamed Item",
"price": "0",
"description": "",
"created_at": "2014-01-16 15:31:36",
"thumbnail": {
"image50": "http://www.example.com/adsa.jpg",
"image100": "hhttp://www.example.com/adsa.jpg"
},...
Am I doing the deserialization right?
public class ItemListModel {
private String user_id;
private String item_id;
private String name;
private String price;
private String category;
private ArrayList<ThumbnailResponse> thumbnail;
public ItemListModel(){}
// getters
}
public class ThumbnailResponse {
private String image50;
private String image100;
public ThumbnailResponse(){
}
//getters
}
I'm just confused, when do we use ArrayList, Array or List for array or object in the JSON file?
One more thing, do I need to make results as an array too if that's the case?
As you have given
"thumbnail": {
"image50": "http://www.example.com/adsa.jpg",
"image100": "hhttp://www.example.com/adsa.jpg"
}
is not a JsonArray. So you have no need to use ThumbnailResponse as an ArrayList into ItemListModel.
Your Model should be
public class ItemListModel {
private String user_id;
private String item_id;
private String name;
private String price;
private String category;
private ThumbnailResponse thumbnail; // Not array List
public ItemListModel(){}
// getters
}
And
One more thing, do I need to make results as an array too if that's
the case?
Your main data container should be contain ArrayList of ItemListModel. Like below
ArrayList<ItemListModel> results = new ArrayList<ItemListModel>();
[] in json -> array
{} in json -> object or map
in your case
// change
private ArrayList<ThumbnailResponse> thumbnail;
// to
private Map<String,String> thumbnail;
if you want it the way you declared your java object you need to provide a transformer (depends on the framework you are using)
List<ItemListModel > ItemListModel ;
try {
Type listType = new TypeToken<List<ItemListModel >>(){}.getType();
result= (List<ItemListModel >) gson.fromJson(result, listType);
} catch (Exception e) {
Log.e("Parsing exeption", e.getLocalizedMessage(), e);
}
this should work