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