Java 解码json android
我很难解码我的json数据。请帮助。以下是我的代码: 这就是我的json的外观:Java 解码json android,java,android,json,gson,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\
[
{"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();
类型fooType=newtypetoken(){}.getType();
傻瓜疫苗=gson.fromJson(json,fooType);
谢谢您的帮助。如果您尝试:
Gson gson = new Gson();
FooList vaccines = gson.fromJson(json, FooList.class);
您得到了什么错误/异常
更新:
public class Foo
{
@SerializedName("0")
String zero;
@SerializedName("ID")
String ID;
@SerializedName("1")
String unu;
@SerializedName("Country")
String Country;
@SerializedName("2")
String doi;
@SerializedName("Age")
String Age;
@SerializedName("3")
String trei;
@SerializedName("Type")
String Type;
@SerializedName("4")
String patru;
@SerializedName("Brands")
String Brands;
@SerializedName("5")
String cinci;
@SerializedName("Contents")
String Contents;
@SerializedName("6")
String sase;
@SerializedName("Time")
String Time;
@Override
public String toString()
{
return String.format(
"{Foo: zero=%s, ID=%s, unu=%s, Country=%s, doi=%s, Age=%s, trei=%s, Type=%s, patru=%s, Brands=%s, cinci=%s, Contents=%s, sase=%s, Time=%s}",
zero, ID, unu, Country, doi, Age, trei, Type, patru, Brands, cinci, Contents, sase, Time);
}
public static void main(String[] args) throws Exception
{
Gson gson = new Gson();
Foo[] foos = gson.fromJson(new FileReader("input.json"), Foo[].class);
System.out.println(Arrays.toString(foos));
}
}
public static void main(String[] args) throws Exception
{
Gson gson = new Gson();
Type listOfFooType = new TypeToken<List<Foo>>(){}.getType();
List<Foo> foos = gson.fromJson(new FileReader("input.json"), listOfFooType);
System.out.println(foos);
}
public class Foo
{
...
public static void main(String[] args) throws Exception
{
Gson gson = new Gson();
FooList fooList = gson.fromJson(new FileReader("input.json"), FooList.class);
System.out.println(fooList.listFoo);
}
}
class FooList
{
List<Foo> listFoo;
}
问题是您试图将项目的列表
解码为包含项目列表的对象。如果您想解码为傻瓜式,那么您的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);
}
你几乎成功了。以下示例均按预期反序列化并运行
反序列化为一个Foo数组:
public class Foo
{
@SerializedName("0")
String zero;
@SerializedName("ID")
String ID;
@SerializedName("1")
String unu;
@SerializedName("Country")
String Country;
@SerializedName("2")
String doi;
@SerializedName("Age")
String Age;
@SerializedName("3")
String trei;
@SerializedName("Type")
String Type;
@SerializedName("4")
String patru;
@SerializedName("Brands")
String Brands;
@SerializedName("5")
String cinci;
@SerializedName("Contents")
String Contents;
@SerializedName("6")
String sase;
@SerializedName("Time")
String Time;
@Override
public String toString()
{
return String.format(
"{Foo: zero=%s, ID=%s, unu=%s, Country=%s, doi=%s, Age=%s, trei=%s, Type=%s, patru=%s, Brands=%s, cinci=%s, Contents=%s, sase=%s, Time=%s}",
zero, ID, unu, Country, doi, Age, trei, Type, patru, Brands, cinci, Contents, sase, Time);
}
public static void main(String[] args) throws Exception
{
Gson gson = new Gson();
Foo[] foos = gson.fromJson(new FileReader("input.json"), Foo[].class);
System.out.println(Arrays.toString(foos));
}
}
public static void main(String[] args) throws Exception
{
Gson gson = new Gson();
Type listOfFooType = new TypeToken<List<Foo>>(){}.getType();
List<Foo> foos = gson.fromJson(new FileReader("input.json"), listOfFooType);
System.out.println(foos);
}
public class Foo
{
...
public static void main(String[] args) throws Exception
{
Gson gson = new Gson();
FooList fooList = gson.fromJson(new FileReader("input.json"), FooList.class);
System.out.println(fooList.listFoo);
}
}
class FooList
{
List<Foo> listFoo;
}
反序列化到Foo的列表中:
public class Foo
{
@SerializedName("0")
String zero;
@SerializedName("ID")
String ID;
@SerializedName("1")
String unu;
@SerializedName("Country")
String Country;
@SerializedName("2")
String doi;
@SerializedName("Age")
String Age;
@SerializedName("3")
String trei;
@SerializedName("Type")
String Type;
@SerializedName("4")
String patru;
@SerializedName("Brands")
String Brands;
@SerializedName("5")
String cinci;
@SerializedName("Contents")
String Contents;
@SerializedName("6")
String sase;
@SerializedName("Time")
String Time;
@Override
public String toString()
{
return String.format(
"{Foo: zero=%s, ID=%s, unu=%s, Country=%s, doi=%s, Age=%s, trei=%s, Type=%s, patru=%s, Brands=%s, cinci=%s, Contents=%s, sase=%s, Time=%s}",
zero, ID, unu, Country, doi, Age, trei, Type, patru, Brands, cinci, Contents, sase, Time);
}
public static void main(String[] args) throws Exception
{
Gson gson = new Gson();
Foo[] foos = gson.fromJson(new FileReader("input.json"), Foo[].class);
System.out.println(Arrays.toString(foos));
}
}
public static void main(String[] args) throws Exception
{
Gson gson = new Gson();
Type listOfFooType = new TypeToken<List<Foo>>(){}.getType();
List<Foo> foos = gson.fromJson(new FileReader("input.json"), listOfFooType);
System.out.println(foos);
}
public class Foo
{
...
public static void main(String[] args) throws Exception
{
Gson gson = new Gson();
FooList fooList = gson.fromJson(new FileReader("input.json"), FooList.class);
System.out.println(fooList.listFoo);
}
}
class FooList
{
List<Foo> listFoo;
}
publicstaticvoidmain(字符串[]args)引发异常
{
Gson Gson=新的Gson();
类型listOfFooType=newTypeToken(){}.getType();
List foos=gson.fromJson(新文件读取器(“input.json”)、listofootype);
系统输出打印LN(foos);
}
使用Tanner更改的JSON反序列化为傻瓜:
public class Foo
{
@SerializedName("0")
String zero;
@SerializedName("ID")
String ID;
@SerializedName("1")
String unu;
@SerializedName("Country")
String Country;
@SerializedName("2")
String doi;
@SerializedName("Age")
String Age;
@SerializedName("3")
String trei;
@SerializedName("Type")
String Type;
@SerializedName("4")
String patru;
@SerializedName("Brands")
String Brands;
@SerializedName("5")
String cinci;
@SerializedName("Contents")
String Contents;
@SerializedName("6")
String sase;
@SerializedName("Time")
String Time;
@Override
public String toString()
{
return String.format(
"{Foo: zero=%s, ID=%s, unu=%s, Country=%s, doi=%s, Age=%s, trei=%s, Type=%s, patru=%s, Brands=%s, cinci=%s, Contents=%s, sase=%s, Time=%s}",
zero, ID, unu, Country, doi, Age, trei, Type, patru, Brands, cinci, Contents, sase, Time);
}
public static void main(String[] args) throws Exception
{
Gson gson = new Gson();
Foo[] foos = gson.fromJson(new FileReader("input.json"), Foo[].class);
System.out.println(Arrays.toString(foos));
}
}
public static void main(String[] args) throws Exception
{
Gson gson = new Gson();
Type listOfFooType = new TypeToken<List<Foo>>(){}.getType();
List<Foo> foos = gson.fromJson(new FileReader("input.json"), listOfFooType);
System.out.println(foos);
}
public class Foo
{
...
public static void main(String[] args) throws Exception
{
Gson gson = new Gson();
FooList fooList = gson.fromJson(new FileReader("input.json"), FooList.class);
System.out.println(fooList.listFoo);
}
}
class FooList
{
List<Foo> listFoo;
}
公共类Foo
{
...
公共静态void main(字符串[]args)引发异常
{
Gson Gson=新的Gson();
doulist-doulist=gson.fromJson(新文件读取器(“input.json”),doulist.class);
System.out.println(愚人主义者.listFoo);
}
}
阶级愚人
{
列出listFoo;
}
原因:com.google.gson.JsonParseException:应为数组,但找到了对象:com.imc.com。fooList@43d4eb00