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.

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

8 Comments

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.
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 -.-''
Check this link. The JSON should be an array. Try sharing your JSON.
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.
Not a problem, I'll get back tomorrow. :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.