java - Google Gson - com.google.gson.JsonSyntaxException : java. lang.IllegalStateException:需要一个字符串,但实际上是 BEGIN_OBJECT

标签 java json gson

这是我尝试反序列化的对象:

public class DomaineBO {

  private String nom;

  private Set<String> codesQC;

  private Set<String> codesSql;

  ...

这是我的 JSon 数据:

[  
   {  
      "id":30,
      "nom":"Usafe",
      "gere":true,
      "codesQC":[  
         {  
            "id":40,
            "nom":"ServicesTransversaux",
            "nomHTML":"ServicesTransversaux"
         },
         {  
            "id":41,
            "nom":"%22Services%20Transversaux%22",
            "nomHTML":"\"Services Transversaux\""
         }
      ],
      "codesSql":[  
         {  
            "id":61,
            "nom":"USAF"
         }
      ]
   },
   {  
      "id":33,
      "nom":"Epublishing",
      "gere":true,
      "codesQC":[  
         {  
            "id":45,
            "nom":"ServicesDocumentaires",
            "nomHTML":"ServicesDocumentaires"
         }
      ],
      "codesSql":[  
         {  
            "id":64,
            "nom":"EDIT"
         }
      ]
   },
   {  
      "id":25,
      "nom":"Commons",
      "gere":true,
      "codesQC":[  
         {  
            "id":34,
            "nom":"Commons",
            "nomHTML":"Commons"
         }
      ],
      "codesSql":[  
         {  
            "id":82,
            "nom":"COM"
         }
      ]
   },
   {  
      "id":22,
      "nom":"Finance",
      "gere":true,
      "codesQC":[  
         {  
            "id":27,
            "nom":"%22Refonte%20Contentieux%22",
            "nomHTML":"\"Refonte Contentieux\""
         },
         {  
            "id":28,
            "nom":"Finance",
            "nomHTML":"Finance"
         },
         {  
            "id":29,
            "nom":"%22Refonte%20Finance%20Client%22",
            "nomHTML":"\"Refonte Finance Client\""
         }
      ],
      "codesSql":[  
         {  
            "id":45,
            "nom":"FINA"
         }
      ]
   },
   {  
      "id":32,
      "nom":"Inconnu",
      "gere":true,
      "codesQC":[  

      ],
      "codesSql":[  

      ]
   },
   {  
      "id":31,
      "nom":"Marketing",
      "gere":true,
      "codesQC":[  
         {  
            "id":42,
            "nom":"Marketing_ActionsCom",
            "nomHTML":"Marketing_ActionsCom"
         },
         {  
            "id":44,
            "nom":"Vente",
            "nomHTML":"Vente"
         },
         {  
            "id":43,
            "nom":"Marketing_Produits",
            "nomHTML":"Marketing_Produits"
         }
      ],
      "codesSql":[  
         {  
            "id":63,
            "nom":"MARK"
         }
      ]
   },
   {  
      "id":26,
      "nom":"Facturation",
      "gere":true,
      "codesQC":[  
         {  
            "id":35,
            "nom":"Facturation",
            "nomHTML":"Facturation"
         }
      ],
      "codesSql":[  
         {  
            "id":54,
            "nom":"FACT"
         }
      ]
   },
   {  
      "id":24,
      "nom":"Sinistre",
      "gere":true,
      "codesQC":[  
         {  
            "id":32,
            "nom":"Sinistre",
            "nomHTML":"Sinistre"
         },
         {  
            "id":33,
            "nom":"Entreprise",
            "nomHTML":"Entreprise"
         }
      ],
      "codesSql":[  
         {  
            "id":47,
            "nom":"SINI"
         }
      ]
   },
   {  
      "id":23,
      "nom":"Partenaire",
      "gere":true,
      "codesQC":[  
         {  
            "id":31,
            "nom":"Partenaire",
            "nomHTML":"Partenaire"
         },
         {  
            "id":30,
            "nom":"Partenaires",
            "nomHTML":"Partenaires"
         }
      ],
      "codesSql":[  
         {  
            "id":46,
            "nom":"PART"
         }
      ]
   },
   {  
      "id":1,
      "nom":"Contrat",
      "gere":true,
      "codesQC":[  
         {  
            "id":24,
            "nom":"Contrat",
            "nomHTML":"Contrat"
         }
      ],
      "codesSql":[  
         {  
            "id":42,
            "nom":"CONT"
         }
      ]
   },
   {  
      "id":29,
      "nom":"Services Transverses",
      "gere":true,
      "codesQC":[  
         {  
            "id":39,
            "nom":"%22Services%20Transversaux%22",
            "nomHTML":"\"Services Transversaux\""
         },
         {  
            "id":38,
            "nom":"ServicesTransversaux",
            "nomHTML":"ServicesTransversaux"
         }
      ],
      "codesSql":[  
         {  
            "id":58,
            "nom":"SECU"
         },
         {  
            "id":57,
            "nom":"DMS"
         },
         {  
            "id":60,
            "nom":"INDE"
         },
         {  
            "id":59,
            "nom":"JBPM"
         }
      ]
   },
   {  
      "id":28,
      "nom":"Flux de données",
      "gere":true,
      "codesQC":[  
         {  
            "id":37,
            "nom":"%22Flux%20de%20donn%C3%A9es%22",
            "nomHTML":"\"Flux de données\""
         }
      ],
      "codesSql":[  
         {  
            "id":81,
            "nom":"FLUX"
         }
      ]
   },
   {  
      "id":27,
      "nom":"Reprise",
      "gere":true,
      "codesQC":[  
         {  
            "id":36,
            "nom":"Reprise",
            "nomHTML":"Reprise"
         }
      ],
      "codesSql":[  
         {  
            "id":55,
            "nom":"FINA"
         }
      ]
   },
   {  
      "id":21,
      "nom":"Batch",
      "gere":true,
      "codesQC":[  
         {  
            "id":25,
            "nom":"Batch",
            "nomHTML":"Batch"
         }
      ],
      "codesSql":[  
         {  
            "id":44,
            "nom":"BATCH"
         }
      ]
   }
]

这是我尝试转换它们的方法:

GsonBuilder builder = new GsonBuilder();
Gson gson = builder.create();
DomaineBO[] infos = (DomaineBO[]) gson.fromJson(getJSonResponse(url), clazz);
Arrays.asList(infos);

当我在类 DomaineBO 中执行转换操作时,我收到 JsonSyntaxException 并显示消息 Expected a string but was BEGIN_OBJECT at line 1 column 51 path $[0] .codesQC[0]

我认为这是因为字符串集的属性。 我可以尝试用数组来实现,但我想知道是否有更好的方法?

最佳答案

这是因为在代码中你有一组字符串。使用字段 id、nom、nomHTML 创建 CodeQC 类并进行更改

Set<String> codesQC 

Set<CodeQC> codesQC

关于java - Google Gson - com.google.gson.JsonSyntaxException : java. lang.IllegalStateException:需要一个字符串,但实际上是 BEGIN_OBJECT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33152108/

相关文章:

java - 仅在 iframe 中设置应用程序状态

java - 使用 httpclient 有没有办法通过 HEAD 请求获取页面的字符集?

java - Java中的@UniqueConstraint注解

php - Paypal REST API 格式错误的 JSON 错误

JSON Oracle SQL 解析/取消嵌套转义形式的嵌入 JSON 数据

java - 使用 Gson 将对象序列化为任意 JSON 数组而不会收到警告

java - 如何从 Java 中格式错误的字符串中获取属性和值

javascript - 如何使用jquery以表格形式动态显示json数据?

python - Tkinter 与 openweather

java - 将GSON中的值放入android中的TextView