-9

By following this blog how can I implement parsing JSON Data like this? Could anyone give me some link/tutorial in retrieving JSON data.

 {
        "batters":
            {
                "batter":
                    [
                        { "id": "1001", "type": "Regular" },
                        { "id": "1002", "type": "Chocolate" },
                        { "id": "1003", "type": "Blueberry" },
                        { "id": "1004", "type": "Devil's Food" }
                    ],
                "batter2":
                    [
                        { "id": "6", "type": "POGI" },
                        { "id": "66", "type": "PWEPWE" },
                        { "id": "666", "type": "SABON" },
                        { "id": "6666", "type": "SABAW" }
                    ]
            },
        "topping":
            [
                { "id": "5001", "type": "None" },
                { "id": "5002", "type": "Glazed" },
                { "id": "5005", "type": "Sugar" },
                { "id": "5007", "type": "Powdered Sugar" },
                { "id": "5006", "type": "Chocolate with Sprinkles" },
                { "id": "5003", "type": "Chocolate" },
                { "id": "5004", "type": "Maple" }
            ]
    }

Here is my parser that i've used in retrieving this data but i'm having problem in getting batters tag and getting to the inner tag of batter/batter2 and possibly the id and type can be multiple..

 /* ---------------------------------------------------------------------------------------------------------------------------- *
    Custom
 * ---------------------------------------------------------------------------------------------------------------------------- */  

    @SuppressWarnings("unchecked")
    public Map<String, ArrayList<List<String>>> JsonToMap(String JsonObj) {
        Map<String, ArrayList<List<String>>> TblMap = new HashMap<String, ArrayList<List<String>>>();
        try {
            List<List<Object>> JObj = (List<List<Object>>) getObjectFromJson(JsonObj);
            for (int x = 0; x < JObj.size(); x++) {

                List<Object> Tbl = new ArrayList<Object>();
                String MapKey = JObj.get(x).get(0).toString();
                Tbl = (List<Object>) JObj.get(x).get(1);
                TblMap.put(MapKey, new ArrayList<List<String>>());
                for (Object obj : Tbl) {
                    TblMap.get(MapKey).add((List<String>) obj); 
                    }
            }
        } catch (JSONException e) { TblMap =  null; }
        return TblMap;
    }

    @SuppressWarnings("unchecked")
    public Map<String, ArrayList<List<Object>>> JsonToMapObject(String JsonObj) {
        Map<String, ArrayList<List<Object>>> TblMap = new HashMap<String, ArrayList<List<Object>>>();
        try {
            List<List<Object>> JObj = (List<List<Object>>) getObjectFromJson(JsonObj);
            for (int x = 0; x < JObj.size(); x++) {

                List<Object> Tbl = new ArrayList<Object>();
                String MapKey = JObj.get(x).get(0).toString();
                Tbl = (List<Object>) JObj.get(x).get(1);
                TblMap.put(MapKey, new ArrayList<List<Object>>());
                for (Object obj : Tbl) {
                    TblMap.get(MapKey).add((List<Object>) obj); 
                    }
            }
        } catch (JSONException e) { TblMap =  null; }
        return TblMap;
    }


    public String GetValue(Map<String, ArrayList<List<String>>> TableCollection, String TableName, Integer RowNumber, Integer ColumnNumber) {
        return TableCollection.get(TableName).get(RowNumber).get(ColumnNumber).toString();  //TableCollection.get(TableName).get(RowNumber).get(ColumnNumber).toString();
    }

    public String MapToJson(Map TMap) {
        JSONObject z = new JSONObject(TMap);
        return z.toString();
    }

    /* ---------------------------------------------------------------------------------------------------------------------------- *
    JSON Natives
 * ---------------------------------------------------------------------------------------------------------------------------- */  

    public String getJsonRepresentation(Object value) throws JSONException {
        if (value == null || value.equals(null)) { return "null"; }
        if (value instanceof Number) { return JSONObject.numberToString((Number) value); }
        if (value instanceof Boolean) { return value.toString(); }
        if (value.getClass().isArray()) {
            StringBuilder sb = new StringBuilder();
            sb.append("[");
            String separator = "";
            for (Object o: (Object[]) value) {
                sb.append(separator).append(getJsonRepresentation(o));
                separator = ",";
            }
            sb.append("]");
            return sb.toString();
        }
        if (value instanceof String) { return value.toString(); }
        return JSONObject.quote(value.toString());
    }

    public Object getObjectFromJson(String jsonString) throws JSONException {
        final Object value = (new JSONTokener(jsonString)).nextValue();
        if (value == null || value.equals(null)) { return null; }
        else if ((value instanceof String) || (value instanceof Number) || (value instanceof Boolean)) { return value; }
        else if (value instanceof JSONArray) { return getListFromJsonArray((JSONArray)value); }
        else if (value instanceof JSONObject) { return getListFromJsonObject((JSONObject)value); }
        throw new JSONException("Invalid JSON string.");
    }

    public List<Object> getListFromJsonArray(JSONArray jArray) throws JSONException {
        List<Object> returnList = new ArrayList<Object>();
        for (int i = 0; i < jArray.length(); i++) { returnList.add(convertJsonItem(jArray.get(i))); }
        return returnList;
    }

    @SuppressWarnings("unchecked")
    public List<Object> getListFromJsonObject(JSONObject jObject) throws JSONException {
        List<Object> returnList = new ArrayList<Object>();
        Iterator<String> keys = jObject.keys();
        List<String> keysList = new ArrayList<String>();
        while (keys.hasNext()) { keysList.add(keys.next()); }
        Collections.sort(keysList);
        for (String key : keysList) {
            List<Object> nestedList = new ArrayList<Object>();
            nestedList.add(key);
            nestedList.add(convertJsonItem(jObject.get(key)));
            returnList.add(nestedList);
        }
        return returnList;
    }

    public Object convertJsonItem(Object o) throws JSONException {
        if (o == null) { return "null"; }   
        if (o instanceof JSONObject) { return getListFromJsonObject((JSONObject) o); }
        if (o instanceof JSONArray) { return getListFromJsonArray((JSONArray) o); }
        if (o.equals(Boolean.FALSE) || (o instanceof String && ((String) o).equalsIgnoreCase("false"))) { return false; }
        if (o.equals(Boolean.TRUE) || (o instanceof String && ((String) o).equalsIgnoreCase("true"))) { return true; }
        if (o instanceof Number) { return o; }
        return o.toString();
    }

    public List<String> getStringListFromJsonArray(JSONArray jArray) throws JSONException {
        List<String> returnList = new ArrayList<String>();
        for (int i = 0; i < jArray.length(); i++) {
            String val = jArray.getString(i);
            returnList.add(val);
        }
        return returnList;
    }

Here is my code in calling my Parser

        Map<String, ArrayList<List<String>>> mapJSON=JsonToMap(convertThisTest);// JsonToMap(String JsonObj)                
        String valuefromJSONObject=GetValue(mapJSON, "batters", 1, 1);
DreamBigAlvin
  • 884
  • 3
  • 13
  • 35
  • 1
    Doesn't the blog have a tutorial on how to parse this data? – Rahul Apr 04 '14 at 08:43
  • possible duplicate of [How to parse JSON in Java](http://stackoverflow.com/questions/2591098/how-to-parse-json-in-java) – Brian Roach Apr 04 '14 at 08:45
  • it doesn't have.. but how can i parse this format.. – DreamBigAlvin Apr 04 '14 at 08:45
  • Please, do not use capital cases in your titles and text. In the internet this is considered as shouting and is impolite. You will not get any positive attention to your problem this way. Use the edit button below question to correct it. Thank you – Ivaylo Slavov Apr 04 '14 at 08:46

1 Answers1

0

Try to use google GSON Library it will fullfill the same requirements as you want.

Here is the Link for a sample tutorial implemented in android how ever the language used is Java All you have to do is make a data model of same type and as members in JSOn. try the example in the link its helpfull

http://www.javacodegeeks.com/2011/01/android-json-parsing-gson-tutorial.html

Umer Kiani
  • 3,783
  • 5
  • 36
  • 63