遍历 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”:“查塔姆广场和沃斯街”,“address_1”:“莫特街12号”,“address_2” :null,"city":"纽约"},"phone":"2126192989","published":"2011-04-07T12:00:33Z","rescinded_at":null,"valid_to":"2011- 04-08T02:00:00Z"}}, {"报价":"到期":"2011-04-08T02:30:00Z","valid_from":"2011-04-07T12:00:31Z","业务":{"address":{"state":"NY","zip":"10013","cross_streets":"查塔姆广场和沃斯街","address_1":"莫特街 12 号"," address_2":null,"city":"纽约"},"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":"新约克"},"电话":"2126192989","发布":"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;
I'm parsing a JSON file that I'm retrieving by accessing an API. Right now, I'm able to create an ArrayList of objects of my Offer class, but I'm only reading the first JSON object and grabbing the strings I'm interested in. How do I go about creating as many of my own Offer objects as there are in the JSON file?
In other words, I need to iterate through the JSON file and grab all of the offers.
The JSON looks like this:
{"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"}}
As you can see, there is one offer object after another...
Here's my code so far:
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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您提供的 JSON 不是有效的 JSON。
如果在开头放置“[”,在末尾放置“]”,则它会成为有效的 JSONArray。
JSONArray JavaDoc
你应该能够做这样的事情:
如果你有Offer JSON 的 JSONObjects 的 JSONArray(如果您像我建议的那样添加括号,那么您就会这样做),然后您可以迭代 JSONArray 的长度,在每次传递中获取 JSONObject,并创建 Offer,就像您在你提供的例子。
The JSON you have presented is not valid JSON.
If you put a '[' at the beginning and a ']' at the end, it becomes a valid JSONArray.
JSONArray JavaDoc
You should be able to do something like this:
If you have a JSONArray of JSONObjects of your Offer JSON (which you would if you add the brackets like I suggested), then you can iterate over the length of the JSONArray, get your JSONObject on each pass, and create the Offer just as you did in the example you provided.