user3800354
user3800354

Reputation: 13

Parse Rest API response

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.

Upvotes: 0

Views: 952

Answers (2)

vivek panchal
vivek panchal

Reputation: 339

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

Upvotes: 2

Jay Dangar
Jay Dangar

Reputation: 3479

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;
}

}

Upvotes: 1

Related Questions