0

Getting below error for the Gson parsing for below JSON

11-05 15:34:00.882: W/System.err(28673): com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 5923

 {
  "lab": [
    [
      {
        "id": "147",
        "messagelab_id": "test",
        "patientlab_ic": "abc",
        "patientlab_name": "some text"

      }
    ]
  ]
}

i have used below model classes for above JSON

public class ScreeningResults implements Serializable{

    private Labs lab;
    //@SerializedName("all_results")
    //private List<LabResult> labResults;

    public Labs getLab() {
        return lab;
    }
    public void setLab(Labs lab) {
        this.lab = lab;
    }


}


public class Labs implements Serializable{

 private List<Lab> lab;

public List<Lab> getLab() {
    return lab;
}

public void setLab(List<Lab> lab) {
    this.lab = lab;
}


}

i am using belo lines to parse serialize above JSON

Gson gson = new Gson();
  ScreeningResults screeningResults=gson.fromJson(response.toString(),ScreeningResults.class);
5
  • your json is invalid,replace "some text", to "some text" Commented Nov 5, 2014 at 10:07
  • Edited assume JSON is valid Commented Nov 5, 2014 at 10:21
  • As your json sturcture is like this "lab": [ [ so it must be List<List<Lab>> Commented Nov 5, 2014 at 10:36
  • Instead of lists, it should also be happy with Lab[][]. When you say Labs lab if looks for "lab" : { /* content defined in Labs class */ }, i.e. an object Commented Nov 5, 2014 at 10:40
  • @zap1 I have chnaged to private List<List<Lab>> lab; not its working Commented Nov 5, 2014 at 12:17

2 Answers 2

2

The JSON contains a two-dimensional array. So instead of List<Lab> you should parse it to List<List<Lab>>.

Sign up to request clarification or add additional context in comments.

Comments

0

Here is the working Domain class for above JSON

@SuppressWarnings("serial")
public class ScreeningResults implements Serializable{

//private Labs lab;
private List<List<Lab>> lab;


public List<List<Lab>> getLab() {
    return lab;
}

public void setLab(List<List<Lab>> lab) {
    this.lab = lab;
}

Comments

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.