2011-06-09 74 views
1
我有一個艱難的時間來解碼的json data.Please help.Here

是我的代碼: 這是我的JSON的樣子:解碼JSON的Android

[ 
    {"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); 

感謝您的幫助。

回答

0

如果你嘗試:

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

你得到什麼錯誤/異常?

UPDATE:

的問題是,你正在試圖將項目的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); 
} 
+0

原因:com.google.gson.JsonParseException:期望數組,但找到對象:[email protected] – user739509 2011-06-09 12:58:11

0

你已經差不多了。以下示例全部反序列化並按預期方式運行。

反序列化爲一個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); 
    } 

反序列化爲用唐納的一個FooList改變JSON:

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