I want to parse these kind of Json responses :
{
\"MyResponse\": {
\"count\": 3,
\"listTsm\": [{
\"id\": \"b90c6218-73c8-30bd-b532-5ccf435da7
You can do simply:
JSONObject response = new JSONObject(resp);
Then you can use depending on the type of the variable something like:
int count = response.getint("count");
or
JSONArray tsm = response.getJSONArray(listTsm)
Then if you want to iterate through the objects inside you use just a for for it.
public static void main(String[] args) throws JSONException {
String jsonString = "{" +
" \"MyResponse\": {" +
" \"count\": 3," +
" \"listTsm\": [{" +
" \"id\": \"b90c6218-73c8-30bd-b532-5ccf435da766\"," +
" \"simpleid\": 1," +
" \"name\": \"vignesh1\"" +
" }," +
" {" +
" \"id\": \"b90c6218-73c8-30bd-b532-5ccf435da766\"," +
" \"simpleid\": 2," +
" \"name\": \"vignesh2\"" +
" }," +
" {" +
" \"id\": \"b90c6218-73c8-30bd-b532-5ccf435da766\"," +
" \"simpleid\": 3," +
" \"name\": \"vignesh3\"" +
" }]" +
" }" +
"}";
JSONObject jsonObject = new JSONObject(jsonString);
JSONObject myResponse = jsonObject.getJSONObject("MyResponse");
JSONArray tsmresponse = (JSONArray) myResponse.get("listTsm");
ArrayList<String> list = new ArrayList<String>();
for(int i=0; i<tsmresponse.length(); i++){
list.add(tsmresponse.getJSONObject(i).getString("name"));
}
System.out.println(list);
}
}
Output:
[vignesh1, vignesh2, vignesh3]
Comment: I didn't add validation
[EDIT]
other way to load json String
JSONObject obj= new JSONObject();
JSONObject jsonObject = obj.fromObject(jsonString);
....