Android:json解析

时间:2011-11-07 05:38:20

标签: android json

我从服务器端得到以下回复>我需要解析它,然后将其存储在数据库中。

  {"navigation_entries":{
   "home":{"forward_icon":"arrowright","link_name":"Home","highlighted_icon_with_text":"home_h","banner_image":"midbanner","children":         ["favorites","calendar","offers","wineries","settings"],"icon_without_text":"home_n","type":"home_page","highlighted_icon_without_text":"home_h","icon_with_text":"home_n"},
   "wineries":{"display_name":"Wineres","line_template":"wineries_list_template","forward_icon":"arrowright","link_name":"Wineries","highlighted_icon_with_text":"wineries_h","icon_without_text":"wineries","type":"leaf_list","leaf_template":"wineries_leaf_template","section":"wineries","icon_with_text":"wineries_n"},
   "more":{"display_name":"More","forward_icon":"arrowright","link_name":"More","highlighted_icon_with_text":"more_h","children":["favorites","calendar","offers","wineries","settings"],"icon_without_text":"more","type":"navigation_list","icon_with_text":"more_n"},
   "offers_all":{"display_name":"Offers : All","line_template":"offers_all_list_template","forward_icon":"arrowright","link_name":"All","children":["offers_all"],"type":"leaf_list","leaf_template":"offers_all_leaf_template","section":"offers_all"},
   "calendar":{"line_template":"calendar_list_template","forward_icon":"arrowright","highlighted_icon_with_text":"calendar_h","icon_without_text":"calendar","list_name":"Calendar","type":"calendar","leaf_template":"calendar_leaf_template","section":"events","icon_with_text":"calendar_n"},
   "offers":{"display_name":"Offers","forward_icon":"arrowright","link_name":"Offers","highlighted_icon_with_text":"offers_h","children":["offers_all"],"icon_without_text":"offers","type":"navigation_list","icon_with_text":"offers_n"}},"type":"navigation"}

我需要获取条目的名称,如home,wine,more,offers_all,calendar,offer或者这些名称每次都在变化的名称,然后获取forwardicon,link_name,highlighted_icon_with_text等的值。 / p>

任何人都可以帮助我吗?

由于

3 个答案:

答案 0 :(得分:1)

您可以使用JSONObjectJSONArray类来解析JSON响应。

并使用以下方法:

  1. getJsonObject()
  2. getJsonArray()
  3. getString()getInt() .....
  4. 更新: 以你的方式尝试,但让我给你起点:

    JSONObject objJson = new JSONObject(jsonString);
    JSONObject objMain = objJson.getJsonObject("navigation_entries");
    
    JSONObject objHome = objMain.getJsonObject("home");
    JSONObject objWineries = objMain.getJsonObject("wineries");
    // And same way for "more","offers_all","calendar", "offers"
    
     // How you get Values/Array from JsonObject
    String strDisplayName = objWineries.getString("display_name");
    String strForwardIcon = objWineries.getString("forward_icon");
    

答案 1 :(得分:0)

android JSONObject类应该能够自动解析所有这些。只是做:

JSONObject myJSON = new JSONObject(your_string);

答案 2 :(得分:0)

检查以下链接Json parsing jar file

从这里我们得到了jar文件,通过使用thta jar文件,我们可以轻松地解析大量数据。