i get a JSON response from a server and i want to transform it into a POJO which is the following:
public class MeasureDataGetPOJO {
#SerializedName("CODE")
private String code = null;
#SerializedName("USER")
private User user = null;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public class User {
private List<MeasureData> measureDatas = null;
public List<MeasureData> getMeasureDatas() {
return measureDatas;
}
public void setMeasureDatas(List<MeasureData> measureDatas) {
this.measureDatas = measureDatas;
}
public class MeasureData {
#SerializedName("MT_TIME")
private String time = null;
#SerializedName("MT_VALUE")
private String value = null;
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
}
}
The JSON i get from the server:
{
"CODE":"012",
"USER":
[
{
"MT_TIME":"1412882760",
"MT_VALUE":"319",
}
]
}
And the error i get from Gson is:
Caused by: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 10
Is there something wrong with the JSON or with the POJO i try to map the JSON into?
I am expecting one user with multiple pairs of MT_TIME and MT_VALUE.
So i get a status code and a user object. the user has a array of pairs of MT_TIME and MT_VALUE.
Later there maybe will be more user informations in the user object.
It is just a guess but would this be the correct json?
{
"CODE":"012",
"USER":
{
"MEASURE_DATA":
[
{
"MT_TIME":"1412882760",
"MT_VALUE":"319"
}
]
}
}
with an additional SerializedName here:
#SerializedName("MEASURE_DATA")
private List<MeasureData> measureDatas = null;
It is expected. From what your class says, the JSON should look like:
{
"CODE": "012",
"USER": {
"MT_TIME": "1412882760",
"MT_VALUE": "319",
}
}
But the user field in the JSON you showed is inside an array.
If this means that what you expect is a list of users instead of just one user, then replace your field in the POJO with a List<User>; otherwise, fix the JSON.
You need list of Users. See that after "USER": there is a [ in your json String.
This is what the error is saying:
Expected BEGIN_OBJECT but was BEGIN_ARRAY
Related
I am consuming a restful API which gives me the following response on a service call,
[
{
id=123,
cloudStatusTimestamp=2019-01-21T15:45:06.823,
cloudStatusCode=null,
cloudStatusMessage=300: PDF generated successfully,
cloudStatusComments=Inbound invoice,Reference: 123
}
,{
id=436,
cloudStatusTimestamp=2019-02-21T05:45:06.423,
cloudStatusCode=null,
cloudStatusMessage=300: PDF generated successfully,
cloudStatusComments=Inbound invoice, Reference: 456
}
]
I want to parse the above response to Java object. I manually tried to convert the response to JSON by replacing '=' by ':'and enclosing key and value pairs with quotes but it didn't work because some values are having ',' in between (cloudStatusComments=Inbound invoice, Reference: 456). Please share your comments.
The format of the JSON is not correct.
the format should be like this :
[
{
"id":123,
"cloudStatusTimestamp":"2019-01-21T15:45:06.823",
"cloudStatusCode":null,
"cloudStatusMessage":"300: PDF generated successfully",
"cloudStatusComments":"Inbound invoice",
"Reference": 123
},
{
"id":436,
"cloudStatusTimestamp":"2019-02-21T05:45:06.423",
"cloudStatusCode":null,
"cloudStatusMessage":"300: PDF generated successfully",
"cloudStatusComments":"Inbound invoice, Reference: 456"
}
]
hope this helps you out
Fisrt of all your json is incorrect, it should be in this format.
[
{
"id":123,
"cloudStatusTimestamp" : "2019-01-21T15:45:06.823",
"cloudStatusCode":null,
"cloudStatusMessage":"300: PDF generated successfully",
"cloudStatusComments":"Inbound invoice",
"Reference": 123
}
,{
"id":436,
"cloudStatusTimestamp":"2019-02-21T05:45:06.423",
"cloudStatusCode":null,
"cloudStatusMessage":"300: PDF generated successfully",
"cloudStatusComments":"Inbound invoice",
"Reference": 456
}
]
Now to parse this json, create following model class and get whatever field you want.
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Example {
#SerializedName("id")
#Expose
private Integer id;
#SerializedName("cloudStatusTimestamp")
#Expose
private String cloudStatusTimestamp;
#SerializedName("cloudStatusCode")
#Expose
private Object cloudStatusCode;
#SerializedName("cloudStatusMessage")
#Expose
private String cloudStatusMessage;
#SerializedName("cloudStatusComments")
#Expose
private String cloudStatusComments;
#SerializedName("Reference")
#Expose
private Integer reference;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCloudStatusTimestamp() {
return cloudStatusTimestamp;
}
public void setCloudStatusTimestamp(String cloudStatusTimestamp) {
this.cloudStatusTimestamp = cloudStatusTimestamp;
}
public Object getCloudStatusCode() {
return cloudStatusCode;
}
public void setCloudStatusCode(Object cloudStatusCode) {
this.cloudStatusCode = cloudStatusCode;
}
public String getCloudStatusMessage() {
return cloudStatusMessage;
}
public void setCloudStatusMessage(String cloudStatusMessage) {
this.cloudStatusMessage = cloudStatusMessage;
}
public String getCloudStatusComments() {
return cloudStatusComments;
}
public void setCloudStatusComments(String cloudStatusComments) {
this.cloudStatusComments = cloudStatusComments;
}
public Integer getReference() {
return reference;
}
public void setReference(Integer reference) {
this.reference = reference;
}
}
i want to parse Json with this format:
{"data": {
"user": {
"edge_follow": {
"count": 2554, "page_info": {
"node": {
"id": "5719761315", "username": "disneyangell" ...
"node": {
"id": "2260368333", "username": "moosa_sedaghat",...
"node": {
"id": "3982701506", "username": "alidadashi512", ...
.
.
.
from this link ;
i got my pojos from www.jsonschema2pojo.org/
i tried GsonConverter and Jackson ObjectMapper also
the problem is parsed object's node list is empty or it's zero always.
how to solve this?
if i need to use CustomConverter write that for this case.
So to get the JSON you want you have to be logged in to instagiam. Otherwise you will get an empty "edges" object in the JSON returned from your get request. If you are logged in here is an example to do it with Gson:
The POJO (maybe you need to add getter methods for fields you are interested in):
public class FollowJson{
Data data;
String status;
public ArrayList<FollowNode> getFollowNodes(){
return data.getFollowNodes();
}
class Data{
User user;
public ArrayList<FollowNode> getFollowNodes(){
return user.getFollowNodes();
}
}
class User{
EdgeFollow edge_follow;
public ArrayList<FollowNode> getFollowNodes(){
return edge_follow.getFollowNodes();
}
}
class EdgeFollow{
Integer count;
ArrayList<OuterNode> edges;
HashMap<String, Object> page_info;
public ArrayList<FollowNode> getFollowNodes(){
ArrayList<FollowNode> bufList = new ArrayList<FollowNode>();
for(OuterNode outer : edges){
bufList.add(outer.getNode());
}
return bufList;
}
}
class OuterNode{
FollowNode node;
public FollowNode getNode(){
return node;
}
}
class FollowNode {
Boolean followed_by_viewer;
String full_name;
String id;
Boolean is_verified;
String profile_pic_url;
Boolean requested_by_viewer;
String username;
public Boolean getFollowedStatus(){
return followed_by_user;
}
public String getId(){
return id;
}
public String getUsername(){
return username;
}
}
}
Then pass the POJO.class and the JSON string to the Method:
public <T> T getJsonFromString(String jsonString, Class<T> var){
GsonBuilder builder = new GsonBuilder();
return builder.create().fromJson(jsonString, var);
}
You can then call getFollowNodes() on the returned object, which returns an array of objects (FollowNode) representing the "nodes" in the JSON.
I have the following json from the Server. It is a json array with different objects. I want to identify the user objects based on the key "type" and add them to a user hashmap and fetch user to show information in my view containing the "payments" object. I am using gson and retrofit. TIA
"included":[
{
"id":"1",
"type":"payments",
"attributes":{
"amount_cents":100,
"amount_currency":"INR",
"description":"Test description!!",
"created_at":"2016-03-01T11:30:53Z",
"status":"paid",
"paid_at":null,
"charged_at":null,
"formatted_amount":"Rs1.00"
},
"relationships":{
"sender":{
"data":{
"id":"2",
"type":"users"
}
},
"receiver":{
"data":{
"id":"1",
"type":"users"
}
}
}
},
{
"id":"2",
"type":"users",
"attributes":{
"first_name":"Rob",
"last_name":"Thomas"
}
},
{
"id":"1",
"type":"users",
"attributes":{
"first_name":"Matt",
"last_name":"Thomas"
}
}]
My classes are
public class ActivityFeedItem implements IFeedItem {
#SerializedName("id")
String id;
#SerializedName("type")
String type;
#SerializedName("attributes")
Attributes attributes;
protected class Attributes {
double amount_cents;
String amount_currency;
String description;
String created_at;
String status;
String paid_at;
String charged_at;
String formatted_amount;
Relationships relationships;
public double getAmount_cents() {
return amount_cents;
}
public String getAmount_currency() {
return amount_currency;
}
public String getDescription() {
return description;
}
public String getCreated_at() {
return created_at;
}
public String getStatus() {
return status;
}
public String getPaid_at() {
return paid_at;
}
public String getCharged_at() {
return charged_at;
}
public String getFormatted_amount() {
return formatted_amount;
}
public Relationships getRelationships() {
return relationships;
}
}
}
and
public class UserFeedItem implements IFeedItem {
#SerializedName("id")
String id;
#SerializedName("type")
String type;
#SerializedName("attributes")
Attributes attributes;
public class Attributes {
#SerializedName("first_name")
String first_name;
#SerializedName("last_name")
String last_name;
}
}
This is pretty easy if you just put your JSON response String into a JSONArray. Then you can just access the type field and test if it's users. Like this:
JSONArray jsonArray = new JSONArray(yourServerResponseString);
for(int i=0; i<jsonArray.length(); i++) {
JSONObject object = (JSONObject) jsonArray.get(i);
String type = object.getString("type");
if(type.equals("users")) {
//add to your users HashMap
}
}
First of all create an array of objects from you JSON using GSON as below
Gson gson= new Gson();
String jsonString= yourJsonObject.getString("included");
ActivityFeedItem[] activityFeedArray=gson.fromJson(jsonString,ActivityFeedItem[].class);
Now your activityFeedArray contains all the feedItems you get in JSON. Then you can iterate through it as you would in any array and add to hashmap when type is user as below-
for(ActivityFeedItem item:activityFeedArray) {
if(item.type.equals("users")) {
//add to your users HashMap
}
}
I have the following JSON to represent the server response for a salt request:
{
"USER":
{
"E_MAIL":"email",
"SALT":"salt"
},
"CODE":"010"
}
And i tried to map it with the following POJO:
public class SaltPOJO {
private String code = null;
private User user = null;
#Override
public String toString() {
return this.user.toString();
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public class User {
private String e_mail = null;
private String salt = null;
#Override
public String toString() {
return this.e_mail + ": " + this.salt;
}
public String getE_mail() {
return e_mail;
}
public void setE_mail(String e_mail) {
this.e_mail = e_mail;
}
public String getSalt() {
return salt;
}
public void setSalt(String salt) {
this.salt = salt;
}
}
}
Now everytime i do this:
Gson gson = new Gson();
SaltPOJO saltPojo = gson.fromJson(json.toString(), SaltPOJO.class);
Log.v("Bla", saltPojo.toString());
The saltPojo.toString() is null. How can i map my JSON into POJO using Gson?
Is the order of my variables important for the Gson mapping?
Is the order of my variables important for the Gson mapping?
No, that's not the case.
How can i map my JSON into POJO using Gson?
It's Case Sensitive and the keys in JSON string should be same as variable names used in POJO class.
You can use #SerializedName annotation to use any variable name as your like.
Sample code:
class SaltPOJO {
#SerializedName("CODE")
private String code = null;
#SerializedName("USER")
private User user = null;
...
class User {
#SerializedName("E_MAIL")
private String e_mail = null;
#SerializedName("SALT")
private String salt = null;
You don't have proper mapping between your getter and setter. If you change your json to something like below, it would work:
{
"user":
{
"email":"email",
"salt":"salt"
},
"code":"010"
}
If you are getting json form third party then unfortunately, you would have to change your pojo or you could use adapter.
I have a Java app that is getting back the following JSON from a 3rd party RESTful web service:
{
"widgets":[
[
{
"id":25128,
"status":"always",
"uuid":"96f62edd-fa8a-4267-8ffb-14af0d37de26"
}
],
[
{
"id":25200,
"status":"always",
"uuid":"78553c9e-398f-495a-8fb8-ada0fb297844"
}
],
[
{
"id":25128,
"status":"never",
"uuid":"b1e3deb2-a842-4cba-8272-458d15efb394"
}
]
]
}
And trying to convert it into a List<Widget> using GSON:
public class Widget {
#SerializedName("id")
private Long id;
#SerializedName("status")
private String status;
#SerializedName("uuid")
private String uuid;
// Getters & setters, etc.
}
Here is my mapper code:
String jsonResponse = getJsonFromWebService();
Gson gson = new Gson();
List<Widget> widgets = gson.fromJson(jsonResponse, new TypeToken<List<Widget>>(){}.getType());
When I run this, I'm getting the following error:
java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2
java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2
Obviously, I either need to manipulate the JSON string before sending it to my GSON mapper code, or I need to configure GSON to handle the "unexpected" JSON, but I'm not sure which is easier/more appropriate. If I need to "massage" the JSON string, not sure what I need to do to make GSON play nicely with it. And if I need to configure GSON, not sure what to do there either. Any ideas? Thanks in advance.
What's wrong is that you're ignoring the root JSON Object with a single JSON Property "widgets". Try deserializing your data into this object instead:
public class WidgetList {
#SerializedName("widgets")
private List<List<Widget>> widgets;
}
Massaging it to the below format works for me
[
{
'id':25128,
'status':'always',
'uuid':'96f62edd-fa8a-4267-8ffb-14af0d37de26' },
{
'id':25200,
'status':'always',
'uuid':'78553c9e-398f-495a-8fb8-ada0fb297844' },
{ 'id':25128,
'status':'never',
'uuid':'b1e3deb2-a842-4cba-8272-458d15efb394'
}
]
As the below demonstrates
public class TryMe {
public static void main(String[] args) {
Gson gson = new Gson();
List<Widget> widgets = gson.fromJson(json,
new TypeToken<List<Widget>>() {
}.getType());
System.out.println(widgets);
}
}
class Widget {
#SerializedName("id")
private Long id;
#SerializedName("status")
private String status;
#SerializedName("uuid")
private String uuid;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
#Override
public String toString() {
return "Widget [id=" + id + ", status=" + status + ", uuid=" + uuid
+ "]";
}
}
Giving the below resp
[Widget [id=25128, status=always, uuid=96f62edd-fa8a-4267-8ffb-14af0d37de26], Widget [id=25200, status=always, uuid=78553c9e-398f-495a-8fb8-ada0fb297844], Widget [id=25128, status=never, uuid=b1e3deb2-a842-4cba-8272-458d15efb394]]