2011-10-28 2 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

좋은 포맷이 당신의 친구입니다 된 JSONObject (org.json.JSONObject)를 사용할 수 있습니다. –

+0

제공된 위의 Json 문자열은 유효하지 않습니다. upls가 올바른 Json 문자열을 제공 할 수 있습니까? 다음 링크를 사용하여 유효성을 확인할 수 있습니다. http://jsonlint.com/ – Basil

+0

위의 JSON 문자열은 유효하지 않습니다. 시작 부분에 lable "show"가 있습니다. –

답변

2

. Android는 원하는 작업을 수행 할 수있는 적절한 라이브러리를 제공합니다.

This은 Android JSON API 사용법을 이해하는 데 필요한 매뉴얼 페이지입니다.

here 자습서에서 JSON 문자열을 구문 분석하는 방법을 알 수 있습니다. 당신이 된 JSONObject는 "GlossList"를 labled 얻으려면 문자열 내에서, 지금

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

: 예는 위의 문자열 인 경우

{ 
    "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 생성자에 전달 구문 분석 할 수 위와 같이하면 다음을 할 수 있습니다 :

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

덕분에 비토 그림자와 +1 .. 도움이됩니다. –

+0

비토 그림자, 줄 수 있어요 안드로이드에서 Google 차트 및 fussion 차트를 사용하는 방법을 알려주세요. 둘 다 사용하는 방법을 알려주십시오. 도움을 주신다면 –

+0

이 대답을 원한다면 제 대답을 받아 들일 수 있습니까? Google과 fussion 차트를 사용한 적이 한번도 없었습니다 ... 죄송합니다. 도와 드릴 수 없습니다 ... 다른 질문을 시도해보십시오. 누군가 도움이 될 것입니다.) –

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 포맷터가 아주 훌륭합니다. 이 텍스트를 쉽게 이해할 수 있어야합니다.

이 데이터를 구문 분석, 당신은