0

I am working on app in which I have to parse below JSON.

Anyone can anyone tell me how to do this?

{
    "navigation_entries": {
        "home": {
            "children": [
                "favorites",
                "calendar",
                "offers",
                "wineries",
                "dining",
                "lodging",
                "things_to_do",
                "weather",
                "settings"
            ],
            "banner_image": "home_page_banner",
            "icon": {
                "small": "icon_home_small",
                "large": "icon_home_large"
            },
            "type": "home_page",
            "forward_icon": {
                "small": "icon_right_arrow_small",
                "large": "icon_right_arrow_large"
            },
            "link_name": "Home"
        },
        "wineries": {
            "display_name": "Wineries",
            "icon": {
                "small": "icon_wineries_small",
                "large": "icon_wineries_large"
            },
            "line_template": "wineries_list_template",
            "forward_icon": {
                "small": "icon_right_arrow_small",
                "large": "icon_right_arrow_large"
            },
            "link_name": "Wineries",
            "type": "leaf_list",
            "leaf_template": "wineries_leaf_template",
            "section": "wineries"
        },
        "dining": {
            "display_name": "Dining",
            "icon": {
                "small": "icon_dining_small",
                "large": "icon_dining_large"
            },
            "line_template": "dining_list_template",
            "forward_icon": {
                "small": "icon_right_arrow_small",
                "large": "icon_right_arrow_large"
            },
            "link_name": "Dining",
            "type": "leaf_list",
            "leaf_template": "dining_leaf_template",
            "section": "dining"
        },
        "offers_dining": {
            "display_name": "Offers => Dining",
            "list_name": "Dining",
            "line_template": "offers_dining_list_template",
            "type": "leaf_list",
            "leaf_template": "offers_dining_leaf_template",
            "forward_icon": {
                "small": "icon_right_arrow_small",
                "large": "icon_right_arrow_large"
            },
            "section": "offers_dining"
        },
        "favorites": {
            "display_name": "Favorites",
            "icon": {
                "small": "icon_favorites_small",
                "large": "icon_favorites_large"
            },
            "type": "favorites",
            "forward_icon": {
                "small": "icon_right_arrow_small",
                "large": "icon_right_arrow_large"
            },
            "link_name": "Favorites"
        },
        "offers": {
            "display_name": "Offers",
            "children": [
                "offers_wineries",
                "offers_dining",
                "offers_lodging",
                "offers_things_to_do"
            ],
            "icon": {
                "small": "icon_offers_small",
                "large": "icon_offers_large"
            },
            "type": "navigation_list",
            "forward_icon": {
                "small": "icon_right_arrow_small",
                "large": "icon_right_arrow_large"
            },
            "link_name": "Offers"
        }
    },
    "type": "navigation"
}
Sufian
  • 6,405
  • 16
  • 66
  • 120
  • 1
    Good formatting is your friend. – Soner Gönül Oct 28 '11 at 07:18
  • The Json string above provided is not a valid one. Can u pls provide the correct Json string. You can make use of the following link to check its validity: http://jsonlint.com/ – Basil Oct 28 '11 at 07:42
  • The JSON string above is not valid beacause there is the lable "show" at the beginning of it – Vito Gentile Oct 28 '11 at 07:44
  • Possible duplicate of [How to parse json string in Android?](http://stackoverflow.com/questions/8091051/how-to-parse-json-string-in-android) – Sufian Jul 17 '16 at 03:06

3 Answers3

2

You can use this website to format your JSON string in a in an easier way to view it. Android provides an appropiate library to do what you want.

This is the manual page you need to comprehend how to use Android JSON API.

And here you can see a tutorial to understand how to parse JSON string.

{
    "glossary": {
        "title": "example glossary",
        "GlossDiv": {
            "title": "S",
            "GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
                    "SortAs": "SGML",
                    "GlossTerm": "Standard Generalized Markup Language",
                    "Acronym": "SGML",
                    "Abbrev": "ISO 8879:1986",
                    "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
                        "GlossSeeAlso": ["GML", "XML"]
                    },
                    "GlossSee": "markup"
                }
            }
        }
    }
}

If the example above is your string, you can parse it passing it to the JSONObject constructor:

String jsonString = "...."; // the above string
try {
    JSONObject obj = new JSONObject(jsonString);
} catch (JSONException e) {
    // This exception is thrown if jsonString is not correctly formatted
}

Now, if you want to get the JSONObject labled "GlossList", inside the string above, you can do this:

JSONObject glossList = obj.getJSONObject("glossary").getJSONObject("GlossDiv").getJSONObject("GlossList");

There is also another possibility: you can also obtain some JSONArray. In our example, the array GlossSeeAlso:

JSONArray glossSee = glossList.getJSONObject("GlossEntry").getJSONObject("GlossDef").getJSONArray("GlossSeeAlso");

To get directly a value of this array, you can use the method getString(int i); if the contend of the array is a JSONObject, you can use the method getJSONObject(int i). You can also use the method getString(String lable) to get directly a string in a JSONObject:

String title = glossDive.getString("title");
String firstElement = glossSee.getString(0);

Other examples are available in the official JSON site.

Vito Gentile
  • 13,336
  • 9
  • 61
  • 96
  • Vito Shadow , Can you please tell me how to use google charts AND fussion charts in android . Please tell me how to use both .Your help will be appreciated –  Oct 28 '11 at 08:08
  • Please, if you like this answer, can you accept my answer? I've never used Google and fussion charts... Sorry, I cannot help you... Try open another question, someone will help you ;) – Vito Gentile Oct 28 '11 at 08:15
0

You can use JSONObject and JSONTokener like this:

String json = "{"
     + "  \"query\": \"Pizza\", "
     + "  \"locations\": [ 94043, 90210 ] "
     + "}";

JSONObject object = (JSONObject) new JSONTokener(json).nextValue();
String query = object.getString("query");
JSONArray locations = object.getJSONArray("locations");
fadisdh
  • 552
  • 1
  • 4
  • 9
0

jsonlint.com has a very good json formatter. this should make your text easily understandable.

for parsing this data, you can use JSONObject (org.json.JSONObject)

josephus
  • 8,284
  • 1
  • 37
  • 57