如何使用GSON取消实现json?出现错误

问题描述 投票:0回答:3

我存储在字符串中的json的结构是:

{
"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 ...."  
     }
}

我正在使用以下代码,但它在最后一行显示错误:

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

错误信息:

com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Unterminated object at line 1 column 29 path $..PostingDate

拜托,我需要帮助。谢谢

android json gson
3个回答
0
投票

首先 - 创建 POJO / Model 类来转换您的响应。

public class SomeModel{

@SerializedName("name")
@Expose
private String name = null;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}

发表你的回应

 Gson gson = new Gson(); 
 SomeModelResponse response = gson.toJson(result, SomeModel.class);

如果您将多次使用 Gson 对象 - 最好创建它的单例,因为每次创建它的新实例时 - 它都会使用大量内存。

添加检查后:

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

另外,不要忘记检查您希望得到什么样的回复。它不应该是一个 Object.class


0
投票

我注意到错误消息包括“$..PostingDate” 但我无法澄清“response.body()”的内容,试试这个:

String jsonString = String.valueOf(object);

=>

String jsonString = object.toString();

并尝试打印出来看看它是什么。


0
投票

创建您的数据模型

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

}

现在解析它

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

© www.soinside.com 2019 - 2024. All rights reserved.