1

JSONの下で解析する必要があるアプリに取り組んでいます。

誰でもこれを行う方法を教えてもらえますか?

{
    "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"
}
4

3 に答える 3

2

このWebサイトを使用して、JSON文字列をより簡単に表示できるようにフォーマットできます。Androidは、あなたがやりたいことをするための適切なライブラリを提供します。

これは、AndroidJSONAPIの使用方法を理解するために必要なマニュアルページです。

そして、ここでは、JSON文字列を解析する方法を理解するためのチュートリアルを見ることができます。

{
    "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"
                }
            }
        }
    }
}

上記の例が文字列の場合、それを解析してJSONObjectコンストラクターに渡すことができます。

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

ここで、上記の文字列内でJSONObjectラベル付きの「GlossList」を取得する場合は、次のように実行できます。

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

別の可能性もあります。JSONArrayを取得することもできます。この例では、配列GlossSeeAlso:

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

この配列の値を直接取得するには、メソッドgetString(int i);を使用できます。配列の競合がJSONObjectの場合は、メソッドを使用できますgetJSONObject(int i)。このメソッドgetString(String lable)を使用して、JSONObjectの文字列を直接取得することもできます。

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

その他の例は、公式のJSONサイトで入手できます。

于 2011-10-28T07:29:19.557 に答える
0

次のようにJSONObjectJSONTokenerを使用できます。

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

JSONObject object = (JSONObject) new JSONTokener(json).nextValue();
String query = object.getString("query");
JSONArray locations = object.getJSONArray("locations");
于 2011-10-28T07:22:27.237 に答える
0

jsonlint.com には、非常に優れた json フォーマッタがあります。これにより、テキストが理解しやすくなります。

このデータを解析するには、JSONObject (org.json.JSONObject) を使用できます。

于 2011-10-28T07:47:24.360 に答える