我是一个对 JSON 解析很陌生的新手,我从一个请求中获取了一个 JSON 字符串,现在需要用 Java 进行解析。我打算使用 json-lib 库来完成这个任务,但由于不熟悉这个库,所以卡住了。我需要提取以下数据:
1. name (hotel name)
2. starRating
3. geoPoint
我用以下Java代码尝试了一下,但是没有得到我需要的结果,请有人帮忙一下...
非常感谢!
Java代码(s是我获取的JSON字符串):
JSONObject json = (JSONObject) JSONSerializer.toJSON(s);
JSONArray jarray = json.getJSONArray("hotels");
for(int i=0 ; i < jarray.size(); i++) {
System.out.println("jarray [" + i + "] --------" + jarray.getString(i));
}
我需要解析的JSON
[
{
"total": 250,
"offset": 0,
"requestID": "-btygi09oxfov",
"locationName": "Paris, France",
"locationLatitude": 48.86,
"locationLongitude": 2.34,
"cityCode": "PARIS_J_FR",
"hotels": [
{
"ypid": "YN10001x300073304",
"id": 56263,
"hotelRateIndicator": "2",
"name": "Renaissance Paris Vendome Hotel",
"brandCode": "69",
"addressLine1": "4 Rue du Mont-Thabor",
"city": "Paris",
"neighborhood": "",
"state": "IdF",
"country": "US",
"cachedPrice": 935,
"geoPoint": [
48.865361,
2.329584
],
"starRating": "5",
"thumbnailUrl": "http://www.orbitz.com//public/hotelthumbnails/53/97/85397/85397_TBNL_1246535840051.jpg",
"total": 250,
"amenities": [
"24",
"31",
"42",
"52",
"9"
],
"telephoneNumbers": [
""
],
"popularity": 837
},
{
"ypid": "YN10001x300073331",
"id": 112341,
"hotelRateIndicator": "3",
"name": "Renaissance Paris Arc de Triomphe Hotel",
"brandCode": "69",
"addressLine1": "39 Avenue de Wagram",
"city": "Paris",
"neighborhood": "",
"state": "IdF",
"country": "US",
"cachedPrice": 633,
"geoPoint": [
48.877107,
2.297451
],
"starRating": "5",
"thumbnailUrl": "http://www.orbitz.com//public/hotelthumbnails/21/72/302172/302172_TBNL_1246535872514.jpg",
"total": 250,
"amenities": [
"24",
"31",
"42",
"9"
],
"telephoneNumbers": [
""
],
"popularity": 796
}
]
}
]