【问题标题】:Converting JSON string to array of objects using Gson使用 Gson 将 JSON 字符串转换为对象数组
【发布时间】:2023-03-08 07:02:01
【问题描述】:

我在使用 Gson 将 JSON 字符串转换为对象数组时遇到问题。我已经尝试了我能找到的一切,但没有任何帮助。 我的代码是:

   public static ProizvodiViewModel GetProizvode(String tip) {
    String strJson = HttpManager.simpleResponseGet("http://192.168.0.15:21951/api/Proizvodi/SearchProizvodiByVrsta", tip);

    Gson gson = new Gson();
    ProizvodiViewModel x = new ProizvodiViewModel();
    x.Proizvodi = new ProizvodViewModel[]{};//tried also with this line commented

    try {
        //1st attempt
        //x.Proizvodi = gson.fromJson(strJson, ProizvodViewModel[].class);

        //2nd
        //Type type = new TypeToken<List<ProizvodViewModel[]>>() {}.getType();
        //x.Proizvodi = gson.fromJson(strJson, type);

        //3rd and so forth (cause many of answers here on SO had almost same idea)
        Type collectionType = new TypeToken<Collection<ProizvodViewModel>>() {}.getType();
        Collection<ProizvodViewModel> enums = gson.fromJson(strJson, collectionType);            
    } catch (Exception e) {
        System.out.println("ERROR IN GSON");
        System.out.println(e.getMessage());
    }
    return x;
}

我放了 try catch 否则应用程序会崩溃,而且我无法读取 println 的内容。

还有我的课:

public class ProizvodViewModel {
   public int Id ;      
   public boolean IsDeleted ;       
   public String Naziv ;
   public float Cijena ;
   public byte[] Slika ;       
   public byte[] SlikaThumb ;       
   public String Status ;
   public int ProizvodDetaljiId ;       
   public int VrstaId ;
}

public class ProizvodiViewModel
{
 public ProizvodViewModel[] Proizvodi;
}

我以 JSON 格式获取数据,您可以在此处看到:http://pastebin.com/6C7936Uq 我正在使用 Android Studio 1.1.0 和 api 16。

编辑:发布已解决的问题。我让我的 api 返回包含 2 个字节数组属性的 json 字符串,这些属性被转换(我不知道如何)为 base64 字符串,我试图将它们映射到字节数组中,这导致了错误。 我用asp写了我的api。 net 应用程序,所以如果有人想进一步解释为什么会发生这种情况,请这样做。

【问题讨论】:

    标签: java android android-studio gson


    【解决方案1】:

    我会使用不带数组的 ProizvodViewModel。如下:

    public class ProizvodViewModel {
       public int Id ;      
       public boolean IsDeleted ;       
       public String Naziv ;
       public float Cijena ;
       public byte[] Slika ;       
       public byte[] SlikaThumb ;       
       public String Status ;
       public int ProizvodDetaljiId ;       
       public int VrstaId ;
    }
    

    然后,创建一个 ProizvodViewModel 列表,如下所示:

    List<ProizvodViewModel> list = gson.fromJson(strJson, new TypeToken<List<ProizvodViewModel>>(){}.getType());
    

    另外,如果您特别需要一个数组,您可以:

    ProizvodViewModel[] array = new ProizvodViewModel[list.size()];
    list.toArray(array);
    

    【讨论】:

    • 在这个需要的时候,Genymotion 需要 2 长的时间来响应。我将尽快开始测试代码并提供反馈。非常感谢您的回答。
    • 不幸的是,它不起作用。我收到此错误: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 72 path $[0].Slika 难道是因为我有字节数组,它认为它是一个字符串?这实际上是一张图片(以字节为单位),我不知道 java 是如何解析它们的,我只有 exp 和 c# 的东西。这只需要在我的大学完成一项简单的任务-.-''
    • 检查这个link。 JSON 应该是一个数组。尝试分享您的 JSON。
    • 我现在必须认真看一些东西,但我不知道需要多长时间。如果我设法解决了这个问题,您是否介意明天再检查一下这些 cmets,只是告诉您它是否有效,并告诉您将此作为我可以接受的答案,作为解决我问题的答案。
    • 没问题,我明天再来。 :)
    猜你喜欢
    • 1970-01-01
    • 2021-10-04
    • 2013-05-19
    • 1970-01-01
    • 1970-01-01
    • 2018-01-20
    • 2017-01-24
    • 1970-01-01
    • 2017-08-11
    相关资源
    最近更新 更多