2011-05-13 31 views
0

我在解析通過訪問API檢索的JSON文件。現在,我可以創建我的Offer類的一個ArrayList對象,但我只是閱讀第一個JSON對象並獲取我感興趣的字符串。我該如何去創建儘可能多的我的Offer對象在JSON文件中有嗎?在JSON文件中迭代多個對象

換句話說,我需要遍歷JSON文件並獲取所有優惠。

的JSON看起來是這樣的:

{"offer":"expiration":"2011-04-08T02:30:00Z","valid_from":"2011-04-07T12:00:31Z","business":{"address":{"state":"NY","zip":"10013","cross_streets":"Chatham Sq & Worth St","address_1":"12 Mott St","address_2":null,"city":"New York"},"phone":"2126192989","published":"2011-04-07T12:00:33Z","rescinded_at":null,"valid_to":"2011-04-08T02:00:00Z"}}, {"offer":"expiration":"2011-04-08T02:30:00Z","valid_from":"2011-04-07T12:00:31Z","business":{"address":{"state":"NY","zip":"10013","cross_streets":"Chatham Sq & Worth St","address_1":"12 Mott St","address_2":null,"city":"New York"},"phone":"2126192989","published":"2011-04-07T12:00:33Z","rescinded_at":null,"valid_to":"2011-04-08T02:00:00Z"}}, {"offer":"expiration":"2011-04-08T02:30:00Z","valid_from":"2011-04-07T12:00:31Z","business":{"address":{"state":"NY","zip":"10013","cross_streets":"Chatham Sq & Worth St","address_1":"12 Mott St","address_2":null,"city":"New York"},"phone":"2126192989","published":"2011-04-07T12:00:33Z","rescinded_at":null,"valid_to":"2011-04-08T02:00:00Z"}}

正如你可以看到,有一個又一個的報價對象...

這裏是我到目前爲止的代碼:

 ArrayList<Offer> offerList = new ArrayList<Offer>(); 

     for(String url: urls) { 
      OAuthConsumer consumer = new DefaultOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET); 
      consumer.setTokenWithSecret("", ""); 

      try { 

       URL url1 = new URL(url); 
       HttpURLConnection request = (HttpURLConnection) url1.openConnection(); 

       // sign the request 
       consumer.sign(request); 

       // send the request 
       request.connect(); 


       String JSONString = convertStreamToString(request.getInputStream()); 

       JSONObject jObject = new JSONObject(JSONString); 

       JSONObject offerObject = jObject.getJSONObject("offer"); 

       String titleValue = offerObject.getString("title"); 
       //System.out.println(titleValue); 

       String descriptionValue = offerObject.getString("description"); 
       //System.out.println(attributeValue); 
       JSONObject businessObject = offerObject.getJSONObject("business"); 
       String nameValue = businessObject.getString("name"); 

       Offer myOffer = new Offer(titleValue, descriptionValue, nameValue); 

       offerList.add(myOffer); 
       Log.v("ArrayList:", offerList.toString()); 

      } catch (Exception e) { 
       e.printStackTrace(); 

      } 
     } 
     return offerList; 

回答

2

您提交的JSON不是有效的JSON。

如果你在開頭加一個'[',結尾加一個']',它就成爲一個有效的JSONArray。

JSONArray JavaDoc

你應該能夠做這樣的事情:

JSONArray array = new JSONArray(inputJSON); 
for(int index = 0; index < array.length(); ++index) { 
    JSONObject offerObject = array.getJSONObject(index); 
    //... your offer calculation...add offer to list... 
} 

如果您的報價JSON的一個JSONObjects(你會如果你加括號像我的建議)的JSONArray ,那麼您可以遍歷JSONArray的長度,在每次傳遞時獲取您的JSONObject,並創建Offer,就像您在提供的示例中一樣。

+0

廢話,你說得對。我用substring在文件的開始和結尾處切斷了「[」和「]」,因爲我認爲我的convertStreamToString方法正在添加它,但事實並非如此。所以我有JSONArray。你能告訴我如何遍歷數組?我遇到了麻煩...... – LuxuryMode 2011-05-13 05:04:47

+0

我編輯了我的帖子。 – 2011-05-13 05:09:29