2010-10-29 92 views
0

,我需要使用Android的JSON解析器解析在Android的這種反應,但事情我無法找到答案的任何地方是:JSON解析響應可以有一個或多個對象

我如何分析數據,例如,如果「行程」可以包含一個或多個類型行程的對象? 如果它包含一個比它返回像這樣,但如果它包含更多它返回與[] 本示例「行程」不能被放置到JsonArray因爲顯然它不是一個數組。 (沒有放在[]右邊?)

我該如何解析?任何例子?

{ 
    "plan":{ 
     "date":"2010-10-20T00:00:00+02:00", 
     "from":{ 
     "name":"Булевар Партизански Одреди", 
     "stopId":"123", 
     "lon":"21.373255285035548", 
     "lat":"42.00736515785779", 
     "geometry":"{\"type\": \"Point\", \"coordinates\": [21.373255285035548,42.00736515785779]}" 
     }, 
     "to":{ 
     "name":"Булевар Партизански Одреди", 
     "stopId":"123", 
     "lon":"21.37228809181389", 
     "lat":"42.00762790595865", 
     "geometry":"{\"type\": \"Point\", \"coordinates\": [21.37228809181389,42.00762790595865]}" 
     }, 
     "itineraries":{ 
     "itinerary":{ 
      "duration":"159000", 
      "startTime":"2010-10-20T00:00:00+02:00", 
      "endTime":"2010-10-20T00:02:39+02:00", 
      "walkTime":"159000", 
      "transitTime":"0", 
      "waitingTime":"0", 
      "walkDistance":"212.6496008849819", 
      "elevationLost":"0.0", 
      "elevationGained":"0.0", 
      "transfers":"0", 
      "legs":{ 
       "leg":{ 
        "@route":"Булевар Партизански Одреди", 
        "@mode":"WALK", 
        "startTime":"2010-10-20T00:00:00+02:00", 
        "endTime":"2010-10-20T00:02:39+02:00", 
        "distance":"212.6496008849819", 
        "from":{ 
        "name":"Булевар Партизански Одреди", 
        "lon":"21.373255285035548", 
        "lat":"42.00736515785779", 
        "geometry":"{\"type\": \"Point\", \"coordinates\": [21.373255285035548,42.00736515785779]}" 
        }, 
        "to":{ 
        "name":"Булевар Партизански Одреди", 
        "lon":"21.37228809181389", 
        "lat":"42.00762790595865", 
        "geometry":"{\"type\": \"Point\", \"coordinates\": [21.37228809181389,42.00762790595865]}" 
        }, 
        "legGeometry":{ 
        "length":"3", 
        "points":"[email protected]{ArI" 
        }, 
        "steps":{ 
        "walkSteps":{ 
         "distance":"212.6496008849819", 
         "streetName":"Булевар Партизански Одреди", 
         "absoluteDirection":"EAST", 
         "stayOn":"false", 
         "becomes":"false", 
         "lon":"21.373255285035548", 
         "lat":"42.00736515785779", 
         "elevation":"" 
        } 
        }, 
        "duration":"159000" 
       } 
      }, 
      "tooSloped":"false" 
     } 
     } 
    }, 
    "requestParameters":{ 
     "entry":[ 
     { 
      "key":"optimize", 
      "value":"QUICK" 
     }, 
     { 
      "key":"time", 
      "value":"9:40 am\"" 
     }, 
     { 
      "key":"wheelchair", 
      "value":"false" 
     }, 
     { 
      "key":"maxWalkDistance", 
      "value":"800.0" 
     }, 
     { 
      "key":"fromPlace", 
      "value":"42.0074711701039,21.3732840843651" 
     }, 
     { 
      "key":"toPlace", 
      "value":"42.0076745404488,21.3723007605583" 
     }, 
     { 
      "key":"date", 
      "value":"10/20/2010" 
     }, 
     { 
      "key":"mode", 
      "value":"TraverseMode (WALK, TRAM, SUBWAY, RAIL, BUS, FERRY, CABLE_CAR, GONDOLA, FUNICULAR, TRANSIT, TRAINISH, BUSISH)" 
     }, 
     { 
      "key":"numItineraries", 
      "value":"3" 
     } 
     ] 
    } 
} 

以下是我使用的第一部分

JSONObject planObject=json.getJSONObject("plan"); 
      Log.i("date",planObject.get("date").toString()); 

      JSONObject fromObject=planObject.getJSONObject("from"); 
      Log.i("from object",fromObject.get("name").toString()); 
      Log.i("from object",fromObject.get("stopId").toString()); 
      Log.i("from object",fromObject.get("lon").toString()); 
      Log.i("from object",fromObject.get("lat").toString()); 

這是菲利克斯寫的例子,它是關於多個「路線」

"itineraries": [ 
    {"duration": "123456", ... }, 
    {"duration": "789012", ... } 
] 

這將是相同的例子但爲一個:

"itineraries": 
{"duration": "123456", ... }, 

因此在第二種情況下沒有JSONArray,因此如果我嘗試使用Felix給解析數組的代碼,它將返回一個錯誤。

所以問題是:檢查值是否可以放在JSONArray中的方法是什麼。是命令optJSONArray(「possibleArrayValues」)!=空使用或有更好的方法,然後做很多如果然後檢查?

回答

1

我不認爲這是合法有一個JSON對象中具有相同名稱的多個鍵。即JSON對象中的鍵應該形成一個集合,而不是一個列表。

當你有更多的行程時,你能舉一個例子嗎?我敢打賭,它看起來是這樣的:

... 
"itineraries": [ 
    {"duration": "123456", ... }, 
    {"duration": "789012", ... } 
] 
... 

如果確實這個樣子的,解析它很簡單:

JSONArray itineraries = planObject.getJSONArray("itineraries"); 
for (int i=0; i < itineraries.length(); i++) { 
    Log.i("TAG", itineraries.getJSONObject(i).getString("duration"); 
} 

如果沒有,你正在使用的API是破碎。無論是解決它,或告訴任何人運行它來解決它:)


編輯:現在我們知道的多項目反應的樣子,這裏是如何解析它:

Object itineraries = planObject.get("itineraries"); 
if (itineraries instanceof JSONObject) { 
    JSONObject itinerary = (JSONObject) itineraries; 
    // right now, itinerary is your single item 
} 
else { 
    JSONArray array = (JSONArray) itineraries; 
    // do whatever you want with the array of itineraries 
} 

未經測試,但應該有效。

+0

不,這不是我的問題... – DArkO 2010-10-29 15:52:10

+0

Flex是正確的......你可以發表一個多個的例子。 – viv 2010-10-29 16:16:13

+0

是的這個作品非常棒!很多。 – DArkO 2010-10-30 02:05:33