【发布时间】:2013-12-04 15:30:58
【问题描述】:
我浏览了论坛并遇到了一些类似的问题,但都没有任何帮助。
所以我从其他人停止的地方开始,这就是问题所在。我正在处理的代码使用 gson 来解析 json 流,这很好而且很清楚,然后将相关数据添加到一个包裹中。如下所示
public class JsonAssets{
String Address;
String Title;
String File;
Category [] Categories;
public void writeToParcel(Parcel paramParcel, int paramInt) {
paramParcel.writeString(Address);
paramParcel.writeString(Title);
paramParcel.writeString(AudioFile);
}
private JsonAsset(Parcel in) {
Address = in.readString();
Title = in.readString();
AudioFile = in.readString();
}
public ContentValues getContentValues(){
ContentValues contentValue = new ContentValues();
contentValue.put("ID", this.Id);
contentValue.put("Title", this.Title);
contentValue.put("address", this.Address);
}
编辑
private class Category{
String CategoryName;
String Description;
String ExternalLink;
String File;
String FileName;
int CategoryID;
int ParentCategoryID;
int Id;
int Image;
public int getCategoryID(){
return this.Id;
}
}
然后使用这些 contentValues 更新数据库。
JSON 看起来像这样:
"assets":[
{
"Address":"Crator1, The Moon",
"Title":"The Moon",
"AudioFile":null,
"Categories":[
{
"CategoryName":"Restaurants",
"Description":"blah blah",
"ExternalLink":"",
"File":"",
"FileName":"0",
"CategoryID":0,
"ParentCategoryID":786,
"Id":334,
"Image":"",
},
我知道 JSON 无效,我只是进行了编辑以适应。问题是我有类别,一个嵌套数组,我不知道如何处理该数组。我只想得到 Categories 数组中的“Id”。
由于类的传递方式,我无法为其创建单独的对象:
JsonReader reader = new JsonReader(new InputStreamReader(is, "UTF-8"));
reader.beginObject();
reader.nextName();
reader.beginArray();
JsonObject obj = null;
while (reader.hasNext()) {
try{
switch(type){
case ASSET_UPDATE:
obj = gson.fromJson(reader, JsonAsset.class);
break;
}
因此,如果有人能告诉我如何处理这个问题,我将不胜感激。如果我的问题道歉不是很清楚,请问我会澄清......提前谢谢
【问题讨论】: