以下是我要解析的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);
答案 0 :(得分:1)
试试这个,你可以直接解析它,因为列表不需要为它创建另一个类
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);
答案 1 :(得分:0)
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;
}
使用此方法并调用如..
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);
我希望它对你有用。
答案 2 :(得分:0)
而不是
BasicDetailsList basicDetailsList=gson.fromJson(r,BasicDetailsList.class);
请尝试
Type listType= new TypeToken<List<BasicDetails>>(){}.getType();
List<BasicDetails> basicDetailsList=gson.fromJson(r,listType);
由于你有jsonArray的基本细节,使用上面的方法可以减少代码中的类文件。但请确保在“r”中你有json。