How to parse JSON response to POJO class in android? - android

I got response from a webservice (See response below) and I want to convert it into POJO class object. I know Gson library for android for parsing JSON to VO (Value object) object, I used this but it gives me null value in every field (as code stated below) but I want only a few of fields from response like "dob" and "password" and my response is to completed (as shown below) so how can i parse this response? Thanks in advance.
Respose: {"row0":"{\"id\":\"1\",\"password\":\"1234\",\"group_id
\":\"1\",\"dob\":\"1981-10-01\",\"gender\":\"f\",\"marital_status\":\"single\",\"children\":
\"\",\"phone\":\"\",\"mobile\":\"\",\"allow_mobile\":\"0\",\"interests\":\"\"}}
Parsing Code :
Gson gson = new Gson();
UserDetailVO vo = gson.fromJson(result, UserDetailVO.class);
Logger.log("Response", vo.toString());
My UseDetailVO Contain only two String field "password" and "dob" and its getter setter method and overriden "toString()" method that prints both value.

***JSON TO POJO**
http://www.jsonschema2pojo.org/ -- open this Site Create pojo class
Target language:
Java
Source type:
JSON
Annotation style:
Gson
Include getters and setters Check
Preview
--------------------------------
Model Class to pass other class
Parcelable*

Related

Json Response conversion Stuff in Android

Below is the model class I am created for my json response :
data class MyModelClass(
val one: String,
val two: String,
val three: String,
val four: Int,
val tempCurrentCharge: List<CurrentCharge>,
val tempCurrentDischarge: List<CurrentDischarge>
)
Now, above you can see that I am getting Arrays of List<CurrentCharge> and List<CurrentDischarge> in my Json response as below :
"tempCurrentCharge": [
{
"temp": -600,
"cRating": 0
},
{
"temp": 0,
"cRating": 10
}
]
Now, I can successfully parse the json response and saved it in my local db (as per my need).
Now I have two feilds in my local db table, one is for CurrentCharge and another if for CurrentDischarge.
I have to save the whole json string as value in this.
Currently It saved as Object.toString() Instead I want to save the whole json string which is as shared above.
The issue is since I have created the pojo class for json parsing, Its parsing the data for tempCurrentCharge and tempCurrentDischarge automatically.
But I want to store the values of json String in it.
So, What I have done is: changed the type of both the variables as String
But then it given me type casting error as :
"BEGIN_ARRAY... found String".
So, Anyone please suggest me how can I achive storing above jsonArray as string json in my local room db field?
Thanks in Advance.
Let your data class remain as it is, the error you are receiving is justified because in the response you are getting an array while you have changed the type in your data class to a String and thus the following error
"BEGIN_ARRAY... found String".
Coming to what you need, you can simply convert it back to JSON once it is parsed and save it then. You will need Gson library and I am sure it is added to your project. If not please add it.
What next you will need is to simply do is this. I am assuming you have an object of the BatteryConfigurationDetails class
val batteryConfig = BatteryConfigurationDetails()
val tempCurrentChargeJson = Gson().toJson(batteryConfig.tempCurrentCharge)
val tempCurrentDischargeJson = Gson().toJson(batteryConfig.tempCurrentDischarge)
To convert it back you can use the following function
Gson().fromJson()
Add the following if you do not have the library already
implementation 'com.google.code.gson:gson:2.8.7'

Keeping some JSON field as String when deserializing using Jackson

I'm using com.fasterxml.jackson.databind with Retrofit to handle the response from the server in my Android app.
Since the JSONObject response is too complicated and contains lots of JSONArray, I want to be able to parse some of those array fields into String instead of creating POJO for each sub-object that those Array could contains.
Is there a way I could just tell Jackson to keep those field as String and not parse them into Entities?

Parse json data using- GSON - android

I got below json response:
{
"session_id":"3c59ba77-8545-4de7-aae0-41a5s1fads19",
"user-info":{"username":"ganesh","website":null,"location":"newyork","bio":null,"ask":null,"name":"ganesh","isSelf":true}
}
how to create model class for this response:
Unable to create object name for user-info
How to parse this data using GSON?
You could create GSON parser with a FieldNamingPolicy
Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES).create();
It would then convert standard Java naming convention userInfo to user-info (illegal in Java as a variable name) and vice versa.
But in your case separators are mixed (once dash, once underscore).
So there is another solution, set the name using annotation
import com.google.gson.annotations.SerializedName;
[..]
#SerializedName("user-info")
User_info userInfo;

convert json string to POJO JAKSON

I am using JAKSON in my Android Project. Now i have a JSON Object in String format. How can i convert this String into a JAVA POJO class.
There are several different ways to archive this:
1) Use org.json.JSONObject, mannually parse data and put it in POJO.
2) Use third party library like GSON and decode your JSON string directly to Object. GSON for example, use annotations to define serrialization/deserrialization rules for Objects/JSONs.
With Jackson:
MyThing thing = new ObjectMapper().readValue(jsonString, MyThing.class);
For an introduction to using Jackson, I recommend taking a look at http://wiki.fasterxml.com/JacksonInFiveMinutes.
ObjectMapper mapper = new ObjectMapper();
PojoClassName Obj = mapper.readValue(JsonString, PojoClassName.class);
P.S JsonString is the String which contains Json and is to be converted to POJO

Parsing a JsonObject

I have this Json model and I want to parse only one object:
{"CodeMsg": "Server Local Time", "server_time": "2012-03-19 19:59:30", "CodeResult": "OK"}
How can I do that?
There are a number of libraries available to parse JSON. Two that are commonly used are:
Gson - http://code.google.com/p/google-gson/
Jackson - http://jackson.codehaus.org/
With both you do this:
Create a plain java object to represent your data - e.g. a class CodeMsg
Use the library to provide the JSON string/stream, and the type (CodeMsg) and an object of that type is created, with its members set according to the JSON (e.g. server_time, CodeResult, etc)
They are very easy to use.
var data = {"CodeMsg": "Server Local Time", "server_time": "2012-03-19 19:59:30", "CodeResult": "OK"};
JSONObject obj1 = new JSONObject(data);
and then use obj1.getJSONString('CodeMsg');

Categories

Resources