'How to unrealize a json using GSON? Get an error
The structure of the json that I have stored in a string is:
{
"0": {
"PDate": "2019-02-25 00:00:00.0000000",
"DDate": "2019-06-25 00:00:00.0000000",
"Document": "FC",
"Direction": "CALLE ...."
},
"1": {
"PDate": "2019-02-25 00:00:00.0000000",
"DDate": "2019-06-25 00:00:00.0000000",
"Document": "FC",
"Direction": "CALLE ...."
}
}
I'm using the following code, but it shows an error in the last line:
if (response.isSuccessful()){
Object object = response.body();
String jsonString = String.valueOf(object);
Gson gson = new Gson();
Type empMapType = new TypeToken<Map<Integer, Object>>() {}.getType();
Map<Integer, Object> nameObjectJson = gson.fromJson(jsonString, empMapType);
}
The error message:
com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Unterminated object at line 1 column 29 path $..PostingDate
Please, I need help. Thanks
Solution 1:[1]
First - create the POJO / Model class to convert your response.
public class SomeModel{
@SerializedName("name")
@Expose
private String name = null;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Declare your response
Gson gson = new Gson();
SomeModelResponse response = gson.toJson(result, SomeModel.class);
If you will use Gson object more than one time - its better to create a singleton of it because every time when you create a new instance of it - it is use a lot of memory.
After add checking:
if(response !=null && response.getName() !=null){
if(response.getName().equalIgnoreCase("some name")){
// show toast Result Success !!! and move to next screen
}
else if(response.getName().equalIgnoreCase("another name")){
// Invalid Response !!! your logic here
}
}
Also don't forget to check what kind of response are you expect to get. It should not be an Object.class
Solution 2:[2]
I have noticed that error message include "$..PostingDate" but I can NOT clarify the content of "response.body()", try this:
String jsonString = String.valueOf(object);
=>
String jsonString = object.toString();
And try to print out to see what it is.
Solution 3:[3]
Create your DataModel
class Example {
@SerializedName("PDate")
@Expose
private String pDate;
@SerializedName("DDate")
@Expose
private String dDate;
@SerializedName("Document")
@Expose
private String document;
@SerializedName("Direction")
@Expose
private String direction;
public String getPDate() {
return pDate;
}
public void setPDate(String pDate) {
this.pDate = pDate;
}
public String getDDate() {
return dDate;
}
public void setDDate(String dDate) {
this.dDate = dDate;
}
public String getDocument() {
return document;
}
public void setDocument(String document) {
this.document = document;
}
public String getDirection() {
return direction;
}
public void setDirection(String direction) {
this.direction = direction;
}
}
Now parse it
Gson gson = new Gson();
String json = "{\n" +
"\"0\": {\n" +
" \"PDate\": \"2019-02-25 00:00:00.0000000\",\n" +
" \"DDate\": \"2019-06-25 00:00:00.0000000\",\n" +
" \"Document\": \"FC\",\n" +
" \"Direction\": \"CALLE ....\" \n" +
" },\n" +
"\"1\": {\n" +
" \"PDate\": \"2019-02-25 00:00:00.0000000\",\n" +
" \"DDate\": \"2019-06-25 00:00:00.0000000\",\n" +
" \"Document\": \"FC\",\n" +
" \"Direction\": \"CALLE ....\" \n" +
" }\n" +
"}";
Type type = new TypeToken<Map<String, Example>>(){}.getType();
Map<String, Example> myMap = gson.fromJson(json, type);
//Log.d("===", myMap.get("0").document);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Edgar Khimich |
Solution 2 | Kexi He |
Solution 3 | shb |