gpt4 book ai didi

java - com.google.gson.JsonSyntaxException : Expected BEGIN_ARRAY but was STRING

转载 作者:太空狗 更新时间:2023-10-29 15:42:11 26 4
gpt4 key购买 nike

我在解析 json 数据时遇到这个错误:

 Expected BEGIN_ARRAY but was STRING at line 1 column 1156

我找不到解决方案。我的 json 数据是:

{
"project": [
{
"abbreviation": "abd",
"customer": "customer1",
"description": "description1",
"icon": "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAC4UlEQVR42sXTXUhTYRgH8NNNXkTdmPlRpltqalgXZlB4hCC6C8VyYjErtbCoNLJiqQmOtqmYCkUXdhNEGSHYuVXCj81N58dcO+rOPt3c2dnOWTubeSPUv6PZx1U3XfTAj4f3gf/zXL0E8d+LoqgEw+RkvnnOWmi22H6Ytx4bHRtLSt2XSKhUqr8vkMKFYS5I8y5LhGemBYEx8xHPp3CICxpoevFcf3//LrVanaDRaBK0Wu0WnU632XdK8x3E1MxcSZR1Cez7ewgM1IEdqEWYasW6yCMajQqBQGDE5/N98Hq9lNvtppxOJ8UwDEXT9LuJiYmThMlkIuMxUVgXQ1KIw5doEGufOcRjMYiiiEgkAp7nwXEcWJaF3+/HysoKnC7XxtTUdA1hNBrJzUvx+Bpi8Thisbh0+XcwFAohyIUQYANS0A2P2wGXyw6GWdyYMo7VEXq9nuR5QSpeCnDgw0GEQ6x0MQA24MeqzwPvwhiWXzeD7q6CrecibL2XYO2s+DrzoGCEGNcbilmfk3e8fYSlXiWWn13F8vMaLD27gsU+JWydFbDcLcRsVTJmFEmYqdym2AuzcreDGNWbij02A79w/wTmlOmYqz74y6zypwzMVmdi9vIfqjNgrk0cJz5OmIoZI8Vb7kkLruVK8rblbpm/dhjz1yX1ObBsuvHD5tt8K3WQGB43FtPDr3hLYxEW6o/CWl8A280jWLydh+XGHDBNWXA+lMOpksHVnAlnSyYcrTLYW+SwtqS9kBZMkrahPoG+Uwh7w1G4m/LgV2Uh2CZDWJ0BQZOOSMd+RDpTIXSlgJdwnWlY7TjwjdGmtRFvBoeK9C9b7fMNx2J0U67oeHRI9D6WiavtcjH4JEvktBKdXJIusrpU0a9NFj2aFNHefiBsUGVcJk6fObunqbq0SF2aX6IpyyY7yrPIrvPZZHdFDtmjyCV7K/O3+lOFNFPIya4KGdlxQUa2l8lPlR9PS/nnz/gdnPcTQcpv7vgAAAAASUVORK5CYII=",
"name": "projectname1",
"plannedEndDate": "2012-05-25T00:00:00+01:00",
"plannedStartDate": "2012-05-23T00:00:00+01:00",
"projectStatus": {
"name": "Opened"
},
"realEndDate": "2012-05-25T00:00:00+01:00",
"realStartDate": "2012-05-23T00:00:00+01:00"
}
]
}

我像这样将图像转换为字节数组:

public static byte[] convertToByteArray(String path) {  
byte[] imageInByte = null;
try{

BufferedImage originalImage =
ImageIO.read(new File(path));

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write( originalImage, "png", baos );
baos.flush();
imageInByte = baos.toByteArray();

baos.close();

}catch(IOException e){
System.out.println(e.getMessage());
}
return imageInByte;
}

我正在将图标从字节数组转换为位图,如下所示:

Bitmap bm = BitmapFactory.decodeByteArray(project.getIcon(), 0,
project.getIcon().length);
DisplayMetrics dm = new DisplayMetrics();

holder.imageProjet.setMinimumHeight(dm.heightPixels);
holder.imageProjet.setMinimumWidth(dm.widthPixels);
holder.imageProjet.setImageBitmap(bm);

反序列化 JSON 响应的代码:

Gson gson = new GsonBuilder().setDateFormat(
"yyyy-MM-dd'T'HH:mm:ssZ").create();

final ProjectContainer container = gson.fromJson(resultat,
ProjectContainer.class);
final ListView lv = (ListView) findViewById(R.id.list);

/**
* Updating parsed JSON data into ListView
* */
adaptateur = new ProjectAdapter(ProjectActivity.this,
R.layout.ligne_project, container);

这是我的 ProjectContainer 类:

  public class ProjectContainer {
@SerializedName("project")
List<Project> projects ;

public List<Project> getProjects() {
return projects;
}

public void setProjects(List<Project> projects) {
this.projects = projects;
}
}

这是项目类:

public class Project implements Serializable {

private static final long serialVersionUID = 1L;
@SerializedName("projectStatus")
private ProjectStatus projectStatus;
@SerializedName("name")
private String name;
@SerializedName("description")
private String description;
@SerializedName("abbreviation")
private String abbreviation;
@SerializedName("customer")
private String customer;
@SerializedName("plannedStartDate")
private Date plannedStartDate;
@SerializedName("plannedEndDate")
private Date plannedEndDate;
@SerializedName("realStartDate")
private Date realStartDate;
@SerializedName("realEndDate")
private Date realEndDate;
@SerializedName("isDeleted")
private Boolean isDeleted;
@SerializedName("icon")
private byte[] icon;


public Project() {
}

public Project(String name, String description, String abbreviation,
String customer, Date plannedStartDate, Date plannedEndDate,
ProjectStatus projectStatus, Date realStartDate, Date realEndDate) {
this.name = name;
this.description = description;
this.abbreviation = abbreviation;
this.plannedStartDate = plannedStartDate;
this.plannedEndDate = plannedEndDate;
this.projectStatus = projectStatus;
this.realStartDate = realStartDate;
this.realEndDate = realEndDate;
this.customer = customer;

}

public String toString() {
return name;
}

public ProjectStatus getProjectStatus() {
return this.projectStatus;
}

public void setProjectStatus(ProjectStatus projectStatus) {
this.projectStatus = projectStatus;
}

public String getName() {
return this.name;
}

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

public String getDescription() {
return this.description;
}

public void setDescription(String description) {
this.description = description;
}

public String getAbbreviation() {
return this.abbreviation;
}

public void setAbbreviation(String abbreviation) {
this.abbreviation = abbreviation;
}

public Date getPlannedStartDate() {
return this.plannedStartDate;
}

public void setPlannedStartDate(Date plannedStartDate) {
this.plannedStartDate = plannedStartDate;
}

public Date getPlannedEndDate() {
return this.plannedEndDate;
}

public void setPlannedEndDate(Date plannedEndDate) {
this.plannedEndDate = plannedEndDate;
}

public Date getRealStartDate() {
return this.realStartDate;
}

public void setRealStartDate(Date realStartDate) {
this.realStartDate = realStartDate;
}

public Date getRealEndDate() {
return this.realEndDate;
}

public void setRealEndDate(Date realEndDate) {
this.realEndDate = realEndDate;
}


public byte[] getIcon() {
return this.icon;
}

public void setIcon(byte[] icon) {
this.icon = icon;
}

public String getCustomer() {
return this.customer;
}
public void setCustomer(String customer) {
this.customer= customer;
}
}

如果你能帮我解决这个问题,我将不胜感激。提前致谢

最佳答案

当您尝试解析字段 "icon" 时抛出异常,因为在您的 JSON 响应中有一个字符串,您尝试将其解析为 byte[] .

因为在你的课上,icon是一个字节数组,当它试图解析字段 "icon" 时它说它“需要一个数组”,但在 JSON 响应中 "icon"不是一个数组(没有任何东西被 [ ] 包围),所以它说“but was a string”...


编辑:就是说,为了修复它,我认为最简单的方法是更改​​ icon 的类型对于 String正确解析它,然后转换为 byte[]其他地方......例如你可以在类中有一个方法,比方说public byte[] getIconAsByteArray() {...} ,即进行转换。

否则,这可能是最优雅的解决方案,您需要编写 custom deserializer .

关于java - com.google.gson.JsonSyntaxException : Expected BEGIN_ARRAY but was STRING,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16566644/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com