在Android上使用GSON解析JSON嵌套数组 [英] Parsing JSON nested array with GSON on Android

查看:198
本文介绍了在Android上使用GSON解析JSON嵌套数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经浏览了论坛,发现了一些类似的问题,但其中没有任何援助。因此,我正在从其他人的位置离开这是问题所在。我正在处理的代码使用gson来解析json流,这很好,并且很清晰,然后将相关数据添加到宗地中。如下所示

  public class JsonAssets {

String Address;
字符串标题;
字符串文件;
类别[]类别;

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;
字符串描述;
字符串ExternalLink;
字符串文件;
字符串文件名;
int类别ID;
int ParentCategoryID;
int Id;
int Image;

public int getCategoryID(){

return this.Id;

}

}

数据库是然后用这些contentValues进行更新。



JSON看起来像这样:

 assets:[
{
Address:Crator1,The Moon,
Title:月亮,
AudioFile:null,
Categories:[
{
CategoryName:Restaurants,
Description:blah blah,
ExternalLink:,
File:,
FileName:0,
CategoryID:0,
ParentCategoryID:786,
Id :334,
Image:,
},

I知道JSON无效,我刚刚编辑以适应。问题是我有类别,嵌套数组,我不知道如何处理该数组。我希望得到的是'数组'中的'Id'。



我不能为它创建一个单独的对象,因为这个类是如何传递的:



JsonReader reader = new JsonReader(new InputStreamReader(is,UTF-8));

  reader.beginObject(); 
reader.nextName();
reader.beginArray();
JsonObject obj = null;

$ b while(reader.hasNext()){
try {
switch(type){
case ASSET_UPDATE:
obj = gson .fromJson(reader,JsonAsset.class);

break;





$ b

所以如果有人能告诉我如何处理这个,我会非常感激它。如果我不太清楚我的问题是否道歉,请问我会澄清......提前致谢

解决方案

所以我在经过几小时和几个小时的令人心碎的研究之后正确地回答了我自己的问题。

如果任何人有同样的问题,只需点击此链接:

http://www.javacreed.com/gson-deserialiser-example/



它处理1:正确解析嵌套数组,它也为我反序列化。


I have looked through the forum and have across some similar question but none of which have been of any aid.

So I am taking up from where someone else has left off and this is where the issue lies. The code I'm working on uses gson to parse the json stream and that's fine and clear, then relevant data is then added to a parcel. Like below

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

EDIT

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;

}

}

The database is then update with these contentValues.

The JSON looks something like this:

"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":"",
            },

I know that JSON isn't valid, I've just edited to suit. The problem is I have Categories, a nested array and I don't know how to handle that array. All I'm looking to get is the 'Id' in the Categories array.

I can't create a seperate object for it because of how the class is passed:

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

So if anyone could tell me how to handle this, I'd greatly appreciate it. If I'm not very clear in my question apologies, just ask and I'll clarify...thanks in advance

解决方案

So I managed to answer my own question correctly after hours and hours of heartbreaking research.

If anyone has the same issue, just follow this link:
http://www.javacreed.com/gson-deserialiser-example/

It took care of 1: Parsing the nested array correctly and it also deserialized it for me also.

这篇关于在Android上使用GSON解析JSON嵌套数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆