0

I have a set of Servlets that pass back array's of objects like

 text=[{"sPK":1,"sName":"foobar","cFlag":0},
       {"sPK":5,"sName":"barfoo","cFlag":1}]

In a java app I am using org.fasterXML.Jackson to read json and to read array's of this nature I use

ObjectMapper m = new ObjectMappar();
SClass[] ss = m.readValue(response.getResponse(), SClass[].class);

If I try something like this using Moshi

JsonAdapter sJsonAdapter = moshi.adapter(sClass[].class);

The app faults, is there a way to accomplish this using Moshi?

Nefarious
  • 458
  • 6
  • 21

1 Answers1

1

If you want to use Moshi create a class called ServletsResponse and add this

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

}

And then you are able to parse it doing :

Moshi moshi = new Moshi.Builder().build();
JsonAdapter<ServletsResponse> jsonAdapter = moshi.adapter(ServletsResponse.class);

ServletsResponse servletResponse = jsonAdapter.fromJson(json);
System.out.println(servletResponse );
Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148