vipul bansal vipul bansal - 11 months ago 43
Android Question

Why is Gson library giving me an Exception in JSON parsing?

Following is my Json array string that I am trying to parse:



public class BasicDetails {
public String Phone_Number;
public String Fax_Number;
public String EmailID;
public String Established_Year;
public String Recognized_Year;
public String Recognized_Number;
public String Affilation_Board_10;
public String Affilation_Board_12;

public BasicDetails(String phone_Number, String fax_Number, String emailID, String established_Year, String recognized_Year, String recognized_Number, String affilation_Board_10, String affilation_Board_12, String isSchool_Shifts, String medium_Code) {
Phone_Number = phone_Number;
Fax_Number = fax_Number;
EmailID = emailID;
Established_Year = established_Year;
Recognized_Year = recognized_Year;
Recognized_Number = recognized_Number;
Affilation_Board_10 = affilation_Board_10;
Affilation_Board_12 = affilation_Board_12;
IsSchool_Shifts = isSchool_Shifts;
Medium_Code = medium_Code;

public String IsSchool_Shifts;
public String Medium_Code;

public String getPhone_Number() {
return Phone_Number;

public String getFax_Number() {
return Fax_Number;

public String getEmailID() {
return EmailID;

public String getEstablished_Year() {
return Established_Year;

public String getRecognized_Year() {
return Recognized_Year;

public String getRecognized_Number() {
return Recognized_Number;

public String getAffilation_Board_10() {
return Affilation_Board_10;

public String getAffilation_Board_12() {
return Affilation_Board_12;

public String getIsSchool_Shifts() {
return IsSchool_Shifts;

public String getMedium_Code() {
return Medium_Code;


public class BasicDetailsList {
public List<BasicDetails> basicDetailsArrayList;

public List<BasicDetails> getBasicDetailsArrayList() {
return basicDetailsArrayList;

public void setBasicDetailsArrayList(List<BasicDetails> basicDetailsArrayList) {
this.basicDetailsArrayList = basicDetailsArrayList;

public BasicDetailsList() {
basicDetailsArrayList = new ArrayList<BasicDetails>();

Following is the code that I am using for parsing.
String variable r contains the json array

Gson gson=new GsonBuilder().create();
BasicDetailsList basicDetailsList=gson.fromJson(r,BasicDetailsList.class);

Answer Source
public static <T> ArrayList<T> JsonParse(T t, String response) {
        // convert String into InputStream
        InputStream in = new ByteArrayInputStream(response.getBytes());
        JsonReader reader;
        ArrayList<T> lcs = new ArrayList<T>();
        try {
            reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
            Gson gson = new Gson();

            while (reader.hasNext()) {

                T cse = (T) gson.fromJson(reader, t.getClass());
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
        } catch (IOException e) {
            // TODO Auto-generated catch block

        return (ArrayList<T>) lcs;

use this method and call like as..

 String response="[{
    "Phone_Number": "-9463232156",
    "Fax_Number": "-",
    "EmailID": "",
    "Established_Year": "1995",
    "Recognized_Year": "0",
    "Recognized_Number": "0",
    "Affilation_Board_10": "PSEB",
    "Affilation_Board_12": "",
    "IsSchool_Shifts": "No",
    "Medium_Code": "Punjabi"

  ArrayList<BasicDetails> arrayListModel = JsonParse(
                        new BasicDetails(), response);

I hope its useful to you.