迭代JSON文件中的多个对象

时间:2011-05-13 04:55:31

标签: java json

我正在解析我正在通过访问API检索的JSON文件。现在,我可以创建一个我的Offer类对象的ArrayList,但我只是读取第一个JSON对象并抓住我感兴趣的字符串。我如何创建自己的Offer 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; 

1 个答案:

答案 0 :(得分: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...
}

如果你有一个JSONArray的JSONObjects你的Offer JSON(如果你像我建议的那样添加括号),那么你可以遍历JSONArray的长度,在每次传递时获取你的JSONObject,并创建Offer正如您在提供的示例中所做的那样。