2011-05-13 3 views
0

API에 액세스하여 검색중인 JSON 파일을 구문 분석하고 있습니다. 지금은 Offer 클래스의 객체에 대한 ArrayList를 만들 수 있지만 첫 번째 JSON 객체를 읽고 관심있는 문자열을 가져 오는 중입니다. 내 제안을 여러 개 만들려면 어떻게해야합니까? 개체가 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

당신은 같은 것을 할 수 있어야한다 : 당신은 (내가 제안처럼 당신이 괄호를 추가 할 경우) 모집 JSON의 JSONObjects의 JSONArray이있는 경우

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... 
} 

을 JSONArray의 길이를 반복하고 각 패스에서 JSONObject를 가져 와서 제공 한 예제에서와 같이 Offer를 만들 수 있습니다.

+0

젠장, 네 말이 맞아. 나는 convertStreamToString 메서드가 그것을 추가하고 있다고 생각했기 때문에 하위 부분을 사용하여 파일의 처음과 끝에 "["과 "]"를 잘라 냈습니다.하지만 그렇지는 않습니다. 그래서 JSONArray 있습니다. 배열을 반복하는 방법을 보여줄 수 있습니까? 나는 문제가있다. ... – LuxuryMode

+0

나는 나의 지위를 편집했다. –