Android 使用GSON解析JSON字符串中具有相同标记的不同对象
我有一个Android 使用GSON解析JSON字符串中具有相同标记的不同对象,android,json,gson,Android,Json,Gson,我有一个REST调用,它返回成功和失败条件的独立的JSON Success - {"server_method_name":"{\"Master_Tag\":{\**"Table\":[{\"CustomerID\":000000}]**}}"} Failure - {"server_method_name":"{\"Master_Tag\":{\**"Table\" : [{\"Status\" : \"False\"}]**}}"} 区别在于“Table”标记中包含不同类型的数据。 我如
REST调用
,它返回成功和失败条件的独立的JSON
Success - {"server_method_name":"{\"Master_Tag\":{\**"Table\":[{\"CustomerID\":000000}]**}}"}
Failure - {"server_method_name":"{\"Master_Tag\":{\**"Table\" : [{\"Status\" : \"False\"}]**}}"}
区别在于“Table”标记中包含不同类型的数据。
我如何解析它并确定“表”标记包含什么
我正在使用GSON库,这是一个Android应用程序
编辑:
阶级结构-
解析服务器方法名
public class ParseCustomerLoginResponseMethodName {
@SerializedName("server_method_name")
private String mMethodName;
/**
*
*/
public ParseCustomerLoginResponseMethodName() {
// TODO Auto-generated constructor stub
}
/**
* @return the methodName
*/
public String getMethodName() {
return this.mMethodName;
}
/**
* @param methodName the methodName to set
*/
public void setMethodName(String methodName) {
this.mMethodName = methodName;
}
}
要分析标记,请执行以下操作-
public class ParseCustomerLoginResponseTag {
@SerializedName("Master_Tag")
private ParseCustomerLoginResponseTable mTable;
/**
*
*/
public ParseCustomerLoginResponseTag() {
// TODO Auto-generated constructor stub
}
/**
* @return the table
*/
public ParseCustomerLoginResponseTable getTable() {
return this.mTable;
}
/**
* @param table the table to set
*/
public void setTable(ParseCustomerLoginResponseTable table) {
this.mTable = table;
}
}
要分析数据以获得成功结果,请执行以下操作-
public class ParseCustomerLoginResponseTable {
@SerializedName("Table")
private ArrayList<CustomerLogin> mList;
/**
*
*/
public ParseCustomerLoginResponseTable() {
// TODO Auto-generated constructor stub
}
/**
* @return the list
*/
public ArrayList<CustomerLogin> getList() {
return this.mList;
}
/**
* @param list the list to set
*/
public void setList(ArrayList<CustomerLogin> list) {
this.mList = list;
}
}
使用上面的类结构,我可以解析成功条件。
我无法确定在何处使用ParseJsonErrorResponse类,类结构是否错误,无法解析错误条件?使您的holder类具有两个属性:
public class Table(){
boolean Status;
long CustomerID;
}
你必须检查它是否为空
public class Table(){
boolean Status;
long CustomerID;
}