java - 解码json android

标签 java android json gson

我很难解码我的 json 数据。请帮忙。这是我的代码: 这就是我的 json 的样子:

[
   {"0":"6","ID":"6","1":"USA","Country":"USA","2":"1","Age":"1","3":"Type 5","Type":"Type 5","4":"Brand5","Brands":"Brand5","5":"ashfghdfhhgfdhhdfhhdfg\t\t\t\t\t\t","Contents":"ashfghdfhhgfdhhdfhhdfg\t\t\t\t\t\t","6":"0000-00-00 00:00:00","Time":"0000-00-00 00:00:00"},
   {"0":"7","ID":"7","1":"Europe","Country":"Europe","2":"1","Age":"1","3":"Type 5","Type":"Type 5","4":"Brand5","Brands":"Brand5","5":"\t\t\t\tafsfsdgfgh\t\t\t\t\t","Contents":"\t\t\t\tafsfsdgfgh\t\t\t\t\t","6":"0000-00-00 00:00:00","Time":"0000-00-00 00:00:00"},
   {"0":"9","ID":"9","1":"USA","Country":"USA","2":"4","Age":"4","3":"Type3","Type":"Type3","4":"Brand4","Brands":"Brand4","5":"sfdsggfhgfhfhg\t\t","Contents":"sfdsggfhgfhfhg\t\t","6":"0000-00-00 00:00:00","Time":"0000-00-00 00:00:00"},
   {"0":"10","ID":"10","1":"Europe","Country":"Europe","2":"6","Age":"6","3":"Type3","Type":"Type3","4":"Brand4","Brands":"Brand4","5":"\t\t\t\t\tsfgfdgfhhgfdhghg\t\t\t","Contents":"\t\t\t\t\tsfgfdgfhhgfdhghg\t\t\t","6":"2011-06-03 16:07:08","Time":"2011-06-03 16:07:08"}
]

这是我的课:

public class Foo {

@SerializedName("0")
public String zero;

@SerializedName("ID")
public String ID;

@SerializedName("1")
public String unu;

@SerializedName("Country")
public String Country;

@SerializedName("2")
public String doi;

@SerializedName("Age")
public String Age;

@SerializedName("3")
public String trei;

@SerializedName("Type")
public String Type;

@SerializedName("4")
public String patru;

@SerializedName("Brands")
public String Brands;

@SerializedName("5")
public String cinci;

@SerializedName("Contents")
public String Contents;

@SerializedName("6")
public String sase;

@SerializedName("Time")
public String Time;
  }

这是我的 Foo 类型的类列表:

public class FooList {

public List<Foo> listFoo;

}

这就是我尝试解码它的方法:

        Gson gson = new Gson();
        Type fooType=new TypeToken<FooList>() { }.getType();
        FooList vaccines=gson.fromJson(json, fooType);

感谢您的帮助。

最佳答案

如果你尝试一下会怎样:

Gson gson = new Gson();
FooList vaccines = gson.fromJson(json, FooList.class);

您遇到什么错误/异常?

更新:

问题是您正在尝试将项目的 List 解码为包含项目列表的对象。如果您想解码为 FooList 那么您的 JSON 字符串应如下所示:

{ "listFoo":
  [
    {"0":"6","ID":"6","1":"USA","Country":"USA","2":"1","Age":"1","3":"Type 5","Type":"Type 5","4":"Brand5","Brands":"Brand5","5":"ashfghdfhhgfdhhdfhhdfg\t\t\t\t\t\t","Contents":"ashfghdfhhgfdhhdfhhdfg\t\t\t\t\t\t","6":"0000-00-00 00:00:00","Time":"0000-00-00 00:00:00"},
    {"0":"7","ID":"7","1":"Europe","Country":"Europe","2":"1","Age":"1","3":"Type 5","Type":"Type 5","4":"Brand5","Brands":"Brand5","5":"\t\t\t\tafsfsdgfgh\t\t\t\t\t","Contents":"\t\t\t\tafsfsdgfgh\t\t\t\t\t","6":"0000-00-00 00:00:00","Time":"0000-00-00 00:00:00"},
    {"0":"9","ID":"9","1":"USA","Country":"USA","2":"4","Age":"4","3":"Type3","Type":"Type3","4":"Brand4","Brands":"Brand4","5":"sfdsggfhgfhfhg\t\t","Contents":"sfdsggfhgfhfhg\t\t","6":"0000-00-00 00:00:00","Time":"0000-00-00 00:00:00"},
    {"0":"10","ID":"10","1":"Europe","Country":"Europe","2":"6","Age":"6","3":"Type3","Type":"Type3","4":"Brand4","Brands":"Brand4","5":"\t\t\t\t\tsfgfdgfhhgfdhghg\t\t\t","Contents":"\t\t\t\t\tsfgfdgfhhgfdhghg\t\t\t","6":"2011-06-03 16:07:08","Time":"2011-06-03 16:07:08"}
  ]
}

如果您别无选择,只能使用您提供的 JSON 字符串,那么您可以进行如下操作:

List listOfObjects = g.fromJson(json, List.class);

for(Object obj : listOfObjects) {
  FooList.listFoo.add((Foo) obj);
}

关于java - 解码json android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6292906/

相关文章:

java - 我无法使用 java 代码下载特定图像

java - next() 已耗尽元素时的并发修改

java - Prometheus 计算每天对特定 Spring Controller 端点的请求

android知道是否安装了应用程序

android - Firebase 动态链接未在 Android 中将我的应用程序作为单独的应用程序打开

android - 如何在 Xamarin iOS/Android 中使用 Azure 媒体服务

json - 来自 JSON 对象的动态 UI

java* 将默认屏幕的位置设置为 (0,0)

javascript - 解析 Highcharts 的 JSON 数据

.net - ServiceStack.Text 在反序列化可为 null 的 bool 值时出现奇怪的行为