【问题标题】:How to unrealize a json using GSON? Get an error如何使用 GSON 实现 json?得到一个错误
【发布时间】:2019-06-23 02:49:02
【问题描述】:

我存储在字符串中的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


    【解决方案1】:

    首先 - 创建 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

    【讨论】:

    • 感谢您的回答,也许我没有很好地解释节点“1”、“2”的名称取决于可以增加或减少的数量。我需要将字符串转换为模型类,我知道它已按照您告诉我的方式完成,但同样它会引发错误。字符串是图像的相同 json 如果节点的标签根据数量增加或减少,那么模型类是什么?谢谢
    • 我没有看到你的 JSON。可能是你之后添加的。检查@shb 答案。他使用了您的 Json 模型中的名称。
    【解决方案2】:

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

    String jsonString = String.valueOf(object);
    

    =>

    String jsonString = object.toString();
    

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

    【讨论】:

    • 非常感谢。我使用 Scalars (com.squareup.retrofit2:converter-scalars) 解决了这个问题。要使用标量,所有数据都是字符串,然后格式化该字符串并解决问题。谢谢
    【解决方案3】:

    创建您的数据模型

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-12
      • 1970-01-01
      相关资源
      最近更新 更多