我有一组Servlet,这些Servlet会传回类似对象的数组
text=[{"sPK":1,"sName":"foobar","cFlag":0},
{"sPK":5,"sName":"barfoo","cFlag":1}]
在Java应用程序中,我使用org.fasterXML.Jackson读取json并读取我使用的这种性质的数组
ObjectMapper m = new ObjectMappar();
SClass[] ss = m.readValue(response.getResponse(), SClass[].class);
如果我使用Moshi尝试类似的事情
JsonAdapter sJsonAdapter = moshi.adapter(sClass []。class);
该应用程序出现故障,有没有办法使用Moshi完成此任务?
答案 0 :(得分:0)
如果您想使用Moshi
,请创建一个名为ServletsResponse
的类并添加
public class ServletsResponse{
@Json(name = "sPK")
private Integer sPK;
@Json(name = "sName")
private String sName;
@Json(name = "cFlag")
private Integer cFlag;
public Integer getSPK() {
return sPK;
}
public void setSPK(Integer sPK) {
this.sPK = sPK;
}
public String getSName() {
return sName;
}
public void setSName(String sName) {
this.sName = sName;
}
public Integer getCFlag() {
return cFlag;
}
public void setCFlag(Integer cFlag) {
this.cFlag = cFlag;
}
}
然后您就可以解析它了:
Moshi moshi = new Moshi.Builder().build();
JsonAdapter<ServletsResponse> jsonAdapter = moshi.adapter(ServletsResponse.class);
ServletsResponse servletResponse = jsonAdapter.fromJson(json);
System.out.println(servletResponse );