为什么 Gson 库在 JSON 解析中给我一个异常?

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

以下是我要解析的 Json 数组字符串:

[{"Phone_Number":"-9463232156","Fax_Number":"-","EmailID":"ghs.dhurfdk@gmail.com","Established_Year":"1995","Recognized_Year":"0","Recognized_Number":"0","Affilation_Board_10":"PSEB","Affilation_Board_12":"","IsSchool_Shifts":"No","Medium_Code":"Punjabi"}]

Class BasicDetails.java

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

class BasicDetailsList.java

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

以下是我用于解析的代码。 字符串变量 r 包含 json 数组

 Gson gson=new GsonBuilder().create();
 BasicDetailsList basicDetailsList=gson.fromJson(r,BasicDetailsList.class);
                 estd.setText(basicDetailsList.basicDetailsArrayList.get(0).Established_Year);
                    phone.setText(basicDetailsList.basicDetailsArrayList.get(0).Phone_Number);
                    fax.setText(basicDetailsList.basicDetailsArrayList.get(0).Fax_Number);
                    email.setText(basicDetailsList.basicDetailsArrayList.get(0).EmailID);
                    affiliation.setText(basicDetailsList.basicDetailsArrayList.get(0).Affilation_Board_10);
                    medium.setText(basicDetailsList.basicDetailsArrayList.get(0).Medium_Code);

试试这个,你可以直接将它解析为列表,不需要为它创建另一个 class

Gson gson = new Gson();
String jsonOutput = "Your JSON String";
Type listType = new TypeToken<List<BasicDetails>>(){}.getType();
List<BasicDetails> basicDetailsList = (List<BasicDetails>)
gson.fromJson(jsonOutput, listType);
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();

            reader.beginArray();
            while (reader.hasNext()) {

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

        return (ArrayList<T>) lcs;
    }

使用此方法并调用 as..

 String response="[{
    "Phone_Number": "-9463232156",
    "Fax_Number": "-",
    "EmailID": "ghs.dhurfdk@gmail.com",
    "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);

希望对你有用。

而不是

BasicDetailsList basicDetailsList=gson.fromJson(r,BasicDetailsList.class);

请尝试

Type listType= new TypeToken<List<BasicDetails>>(){}.getType();
List<BasicDetails> basicDetailsList=gson.fromJson(r,listType);

因为你有 json 基本细节数组,使用上面的方法你可以从你的代码中减少一个 class 文件。但请确保在 "r" 中你有 json.