2

I'm having issue with conversion of JSON string into array of objects using Gson. I have tried everything I could have find and nothing helped. My code is:

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

I had put try catch cause app would break otherwise, and I couldnt read println's.

And my classes:

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

I get data in JSON ,as you can see here: http://pastebin.com/6C7936Uq I am using Android Studio 1.1.0, and api 16.

Edit: Post solved problem. I had my api return json string containing 2 properties of byte arrays, which were converted (I don't know how) into base64 string and I was trying to map them into byte array ,which was causing error. I wrote my api in asp. net application, so if anyone cares to further explain why this happened, please do.

Kadaj
  • 374
  • 3
  • 13
  • 22

1 Answers1

1

I would use ProizvodViewModel without the array. As follows:

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

then, create a List of ProizvodViewModel, like this:

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

Also, if specifically you need a array, you could:

ProizvodViewModel[] array = new ProizvodViewModel[list.size()];
list.toArray(array);
Amg91
  • 132
  • 5
  • 22
  • In this time of need, Genymotion is taking 2 long to respond. I will test code asap it starts and give feedback. Thank you a lot for answering. – Kadaj Aug 16 '16 at 20:33
  • It isn't working unfortunately. I get this error: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 72 path $[0].Slika Could it be because i have byte array and it thinks that it is a string? This is actually a picture (in bytes) , i don't know how java parses them, I have only exp with c# stuff. This is only needed for 1 simple task at my college -.-'' – Kadaj Aug 16 '16 at 20:39
  • Check this [link](http://stackoverflow.com/a/28418787/6723193). The JSON should be an array. Try sharing your JSON. – Amg91 Aug 16 '16 at 20:41
  • I will seriously have to look at some stuff now, but I don't know how long it will take. If I manage to fix this issues , would you mind to check back tomorrow these comments, just to tell you if it worked and to tell you to put this as an answer I can accept as one that solved my problem. – Kadaj Aug 16 '16 at 20:45
  • Not a problem, I'll get back tomorrow. :) – Amg91 Aug 16 '16 at 20:47
  • Hey mate, I have fixed the issue. Problem was indeed in a picture, since for some reason when I was sending data via api array of bytes was automatically converted into string. Can you put this in answer which I can accept :D Or if you want me to accept this one ,on top , just say it. – Kadaj Aug 17 '16 at 07:47
  • It could be great if you could accept. Not a problem if you don't. Also, great to know you managed to solve your problem. Next time try to add information as much as possible to get more idea of what you are trying. Happy learning! :) – Amg91 Aug 17 '16 at 13:01
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/121170/discussion-between-kadaj-and-amg91). – Kadaj Aug 17 '16 at 13:19