2013-03-01 53 views
1

基本上我想要做的是通過使用類別過濾來自foursquare的場地,但ii想讓用戶選擇他們想要的那種'過濾器',但到目前爲止我不能正確解析json。如何解析foursquare分類json

原始JSON數據,例如

{ 
"meta": { 
"code": 200 
}, 
"response": { 
"categories": [ 
    { 
    "id": "4d4b7104d754a06370d81259", 
    "name": "Arts & Entertainment", 
    "pluralName": "Arts & Entertainment", 
    "shortName": "Arts & Entertainment", 
    "icon": { 
     "prefix": "https:\/\/foursquare.com\/img\/categories_v2\/arts_entertainment\/default_", 
     "suffix": ".png" 
    }, 
    "categories": [ 
     { 
     "id": "4fceea171983d5d06c3e9823", 
     "name": "Aquarium", 
     "pluralName": "Aquariums", 
     "shortName": "Aquarium", 
     "icon": { 
      "prefix": "https:\/\/foursquare.com\/img\/categories_v2\/arts_entertainment\/aquarium_", 
      "suffix": ".png" 
     }, 
     "categories": [ 

     ] 
     }, 
     { 
     "id": "4bf58dd8d48988d1e1931735", 
     "name": "Arcade", 
     "pluralName": "Arcades", 
     "shortName": "Arcade", 
     "icon": { 
      "prefix": "https:\/\/foursquare.com\/img\/categories_v2\/arts_entertainment\/arcade_", 
      "suffix": ".png" 
     }, 
     "categories": [ 

     ] 
     }, 
     { 
     "id": "4bf58dd8d48988d1e2931735", 
     "name": "Art Gallery", 
     "pluralName": "Art Galleries", 
     "shortName": "Art Gallery", 
     "icon": { 
      "prefix": "https:\/\/foursquare.com\/img\/categories_v2\/arts_entertainment\/artgallery_", 
      "suffix": ".png" 
     }, 
     "categories": [ 

     ] 
     }, 
     { 
     "id": "4bf58dd8d48988d1e4931735", 
     "name": "Bowling Alley", 
     "pluralName": "Bowling Alleys", 
     "shortName": "Bowling Alley", 
     "icon": { 
      "prefix": "https:\/\/foursquare.com\/img\/categories_v2\/arts_entertainment\/bowling_", 
      "suffix": ".png" 
     }, 
     "categories": [ 

     ] 
     }, 
     { 
     "id": "4bf58dd8d48988d17c941735", 
     "name": "Casino", 
     "pluralName": "Casinos", 
     "shortName": "Casino", 
     "icon": { 
      "prefix": "https:\/\/foursquare.com\/img\/categories_v2\/arts_entertainment\/casino_", 
      "suffix": ".png" 
     }, 
     "categories": [ 

     ] 
     }, 
     { 
     "id": "4bf58dd8d48988d18e941735", 
     "name": "Comedy Club", 
     "pluralName": "Comedy Clubs", 
     "shortName": "Comedy Club", 
     "icon": { 
      "prefix": "https:\/\/foursquare.com\/img\/categories_v2\/arts_entertainment\/comedyclub_", 
      "suffix": ".png" 
     }, 
     "categories": [ 

     ] 

bla bla bla 

,並使用我現在已經爲下面的代碼:

   JSONObject jsonObj = new JSONObject(response); 
      jsonObj = jsonObj.getJSONObject("response"); 
      JSONArray groups;// = (JSONArray) jsonObj.getJSONObject("response").getJSONArray("Categories); 
      groups = jsonObj.getJSONArray("categories"); 

      FileOutputStream fos = WhereToEatLehActivity.this.openFileOutput("file_name"+".txt",Context.MODE_PRIVATE); 
       Writer out = new OutputStreamWriter(fos); 
       out.write(jsonObj.toString()); 
       out.close(); 
      int length   = jsonObj.length(); 


      if (length > 0) { 

       for (int i = 0; i < length; i++) { 
        JSONObject aCat = (JSONObject) groups.get(i); 
        Log.d("category", aCat.toString()); 
        if(aCat.getString("name").equalsIgnoreCase("Food")){ 
         Log.d("food", aCat.getString("id")); 
        } 


       } 

,並從檢查的txt文件,該文件是從運行輸出,我的JSON是如下:

"categories": [ 
{ 
    "id": "4d4b7104d754a06370d81259", 
    "icon": { 
    "suffix": ".png", 
    "prefix": "https:\/\/foursquare.com\/img\/categories_v2\/arts_entertainment\/default_" 
    }, 
    "categories": [ 
    { 
     "id": "4fceea171983d5d06c3e9823", 
     "icon": { 
     "suffix": ".png", 
     "prefix": "https:\/\/foursquare.com\/img\/categories_v2\/arts_entertainment\/aquarium_" 
     }, 
     "categories": [ 

     ], 
     "shortName": "Aquarium", 
     "pluralName": "Aquariums", 
     "name": "Aquarium" 
    }, 
    { 
     "id": "4bf58dd8d48988d1e1931735", 
     "icon": { 
     "suffix": ".png", 
     "prefix": "https:\/\/foursquare.com\/img\/categories_v2\/arts_entertainment\/arcade_" 
     }, 
     "categories": [ 

     ], 
     "shortName": "Arcade", 
     "pluralName": "Arcades", 
     "name": "Arcade" 
    }, 

,並從那裏,作爲JSON數據下調JSON數據是錯誤已經太多,我不能使用該名稱,但我不知道我做錯了什麼,任何指針傢伙?

回答

1

變化

int length = jsonObj.length(); 

int length = groups.length(); 

使用數內JsonArray,而不是在循環條件

+0

這個工作JsonObject項目!謝謝! – eddy 2013-03-08 04:19:33