2011-10-28 26 views
1

私はJSONの下で解析する必要があるアプリケーションに取り組んでいます。この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" 
} 
+1

あなたのお友達は良いフォーマットです。 –

+0

上記のJson文字列は有効ではありません。正しいJson文字列を提供できますか? http://jsonlint.com/ – Basil

+0

上記のJSON文字列は有効ではありません。先頭にlable "show"があります。 –

答えて

2

。 Androidは、あなたが望むことを行うための適切なライブラリを提供します。

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

そしてhere 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)メソッドを使用できます。また、JSONObjectに直接文字列を取得する方法getString(String lable)を使用することができます。

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

他の例としては、official JSON siteでご利用いただけます。

+0

Vito Shadowに+1して助けてください。 –

+0

Vito Shadow、Can YouアンドロイドでGoogleチャートとフュージョンチャートを使用する方法を教えてください。両方の使い方を教えてください。あなたの助けを歓迎します –

+0

この回答が好きなら、私の答えを受け入れることができますか? 私はGoogleとフュージョンチャートを一度も使用していません...申し訳ありませんが、私はあなたを助けることができません...別の質問を開いてみてください、誰かがあなたを助けます;) –

0

あなたはこのようJSONObjectJSONTokenerを使用することができます:あなたはそれを表示する簡単な方法であなたにJSON文字列をフォーマットするthis websiteを使用することができます

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

JSONObject object = (JSONObject) new JSONTokener(json).nextValue(); 
String query = object.getString("query"); 
JSONArray locations = object.getJSONArray("locations"); 
0

jsonlint.comには非常に優れたjsonフォーマッタがあります。これはあなたのテキストを容易に理解できるようにするはずです。

このデータを解析するには、JSONObject(org.json.JSONObject)を使用してください。

関連する問題