0

I get this JSON response from server in my Android application and I want to parse this data by Gson , In Json Response permission tag of my may contains 20-25 data with key like 1, 2,... every value of the permission contains two information separated by # key I have to display only those things which comes into permission in response. Parsed Object Class(Pojo or Model Class must be Parcelable implemented ) My application also support offline feature so, Need suggestion how I can store data in DB at same time when parsing.

[{
    "permission": {
        "0": "Title#RaqStar",
        "1": "Desc#Reader of Raw Star",
        "3": "Tytpe#Entertainment",
        "4": "Rating#4.5"
    },
    "id": "233",
    "name": "Movie Raw Star"
}, {
    "permission": {
        "0": "Title#RaqStar",
        "3": "Tytpe#Entertainment",
        "4": "Rating#4.5"
    },
    "id": "233",
    "name": "Movie Raw Star"
}, {
    "permission": {
        "1": "Desc#Reader of Raw Star",
        "3": "Tytpe#Entertainment",
        "4": "Rating#4.5"
    },
    "id": "233",
    "name": "Movie Raw Star"
}, {
    "permission": {
        "1": "Desc#Reader of Raw Star",
        "4": "Rating#4.5"
    },
    "id": "233",
    "name": "Movie Raw Star"
}]
chotemotelog
  • 233
  • 1
  • 4
  • 13

1 Answers1

0
    //  Whole response array I took as a String "data"
    try {
        JSONArray array = new JSONArray(data);
        for (int i = 0; i < array.length(); i++) {
             responseJSonObj = array.getJSONObject(i);
             System.out.println(
             generateTitleInfoMap(responseJSonObj.
             getJSONObject("permission")));
        }

    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


public static HashMap<String, String> generateTitleInfoMap(JSONObject titleInfoMap) {
    try {
        HashMap<String, String> mapdataVal = new HashMap<String, String>();

        if (titleInfoMap.length() > 0) {
            Iterator keys = titleInfoMap.keys();
            while (keys.hasNext()) {
                // loop to get the dynamic key
                String currentDynamicKey = (String) keys.next();
                String[] ar = (titleInfoMap.getString(currentDynamicKey)).split("#");

                mapdataVal.put(ar[0], ar[1]);
            }
        }
        return mapdataVal;
    } catch (JSONException e) {

    }
    return null;
}
chotemotelog
  • 233
  • 1
  • 4
  • 13