Android JSON parsing using the Gson Library

时间:2016-12-02 05:11:55

标签: java json gson

i want to parse the following data using the Gson library.

{
    "claimTypeList": [
        "Dental",
        "Outpatient",
        "Hospitalisation/Surgery"
    ]
}

2 个答案:

答案 0 :(得分:2)

@Generated("org.jsonschema2pojo")
public class Example {

@SerializedName("claimTypeList")
@Expose
private List<String> claimTypeList = new ArrayList<String>();

/**
*
* @return
* The claimTypeList
*/
public List<String> getClaimTypeList() {
return claimTypeList;
}

/**
*
* @param claimTypeList
* The claimTypeList
*/
public void setClaimTypeList(List<String> claimTypeList) {
this.claimTypeList = claimTypeList;
}

}

http://www.jsonschema2pojo.org/

答案 1 :(得分:2)

Create a model class like this

public class Claims {
    List<String> claimTypes; 
}

And here convert your string to object

String data = "{\"claimTypeList\":[\"Dental\",\"Outpatient\",\"Hospitalisation/Surgery\"]}"
Claims c = new Claims();
c = new Gson().FromJson(data, Claims.class);

hope this may help.