我目前正在使用以下JSON解析器,但不断收到错误引起:java.lang.ClassCastException:java.lang.String无法强制转换为org.json.JSONObject。我怎么能解决这个问题,它以前已经工作了很多次。
ublic List<String> handleResponse(HttpResponse response)
throws ClientProtocolException, IOException {
List<String> result = new ArrayList<String>();
String JSONResponse = new BasicResponseHandler()
.handleResponse(response);
try {
// Get top-level JSON Object - a Map
JSONObject responseObject = (JSONObject) new JSONTokener(
JSONResponse).nextValue(); //Problem Line
JSONObject item1 = responseObject.getJSONObject("searchResult");
// Extract value of "Item" key -- a List
JSONArray items = item1
.getJSONArray("Item");
以下是我要解析的内容
{
"findItemsAdvancedResponse":[
{
"ack":[
"Success"
],
"version":[
"1.13.0"
],
"timestamp":[
"2015-02-24T18:43:51.778Z"
],
"searchResult":[
{
"@count":"100",
"item":[
{
"itemId":[
"381098176861"
],
"title":[
" Tokyo Laundry Women's Kenzie Sweatpants - Eclipse Blue - TP1"
],
"globalId":[
"EBAY-GB"
],
"subtitle":[
"FREE UK DELIVERY | DISPATCHED WITHIN 1 WORKING DAY"
],
"primaryCategory":[
{
"categoryId":[
"137085"
],
"categoryName":[
"Activewear"
]
}
],
"secondaryCategory":[
{
"categoryId":[
"155226"
],
"categoryName":[
"Hoodies & Sweats"
]
}
],
"galleryURL":[
"http:\/\/thumbs2.ebaystatic.com\/pict\/3810981768614040_1.jpg"
],
"viewItemURL":[
"http:\/\/www.ebay.co.uk\/itm\/Tokyo-Laundry-Womens-Kenzie-Sweatpants-Eclipse-Blue-TP1-\/381098176861?pt=LH_DefaultDomain_3&var="
],
"paymentMethod":[
"PayPal"
],
"autoPay":[
"true"
],
"location":[
"United Kingdom"
],
"country":[
"GB"
],
"shippingInfo":[
{
"shippingServiceCost":[
{
"@currencyId":"GBP",
"__value__":"0.0"
}
],
"shippingType":[
"Free"
],
"shipToLocations":[
"AU",
"Europe"
]
}
],
"sellingStatus":[
{
"currentPrice":[
{
"@currencyId":"GBP",
"__value__":"14.99"
}
],
"convertedCurrentPrice":[
{
"@currencyId":"GBP",
"__value__":"14.99"
}
],
"sellingState":[
"Active"
],
"timeLeft":[
"P25DT14H10M23S"
]
}
],
"listingInfo":[
{
"bestOfferEnabled":[
"false"
],
"buyItNowAvailable":[
"false"
],
"startTime":[
"2014-12-22T08:49:14.000Z"
],
"endTime":[
"2015-03-22T08:54:14.000Z"
],
"listingType":[
"FixedPrice"
],
"gift":[
"false"
]
}
],
"galleryPlusPictureURL":[
"http:\/\/galleryplus.ebayimg.com\/ws\/web\/381098176861_1_1_1.jpg"
],
"condition":[
{
"conditionId":[
"1000"
],
"conditionDisplayName":[
"New with tags"
]
}
],
"isMultiVariationListing":[
"true"
],
"topRatedListing":[
"false"
]
},
{
"itemId":[
"131321072184"
],
"title":[
"Brave Soul Women's Dallas Parka - Mulberry - DD6 - Size 8-16"
],
"globalId":[
"EBAY-GB"
],
"subtitle":[
"FREE UK DELIVERY | DISPATCHED WITHIN 1 WORKING DAY"
],
"primaryCategory":[
{
"categoryId":[
"137085"
],
"categoryName":[
"Activewear"
]
}
],
"secondaryCategory":[
{
"categoryId":[
"63862"
],
"categoryName":[
"Coats & Jackets"
]
}
],
"galleryURL":[
"http:\/\/thumbs1.ebaystatic.com\/pict\/1313210721844040_3.jpg"
],
"viewItemURL":[
"http:\/\/www.ebay.co.uk\/itm\/Brave-Soul-Womens-Dallas-Parka-Mulberry-DD6-Size-8-16-\/131321072184?pt=LH_DefaultDomain_3&var="
],
"paymentMethod":[
"PayPal"
],
"autoPay":[
"true"
],
"location":[
"United Kingdom"
],
"country":[
"GB"
],
"shippingInfo":[
{
"shippingServiceCost":[
{
"@currencyId":"GBP",
"__value__":"0.0"
}
],
"shippingType":[
"Free"
],
"shipToLocations":[
"AU",
"Europe"
]
}
],
"sellingStatus":[
{
"currentPrice":[
{
"@currencyId":"GBP",
"__value__":"19.99"
}
],
"convertedCurrentPrice":[
{
"@currencyId":"GBP",
"__value__":"19.99"
}
],
"sellingState":[
"Active"
],
"timeLeft":[
"P15DT14H57M21S"
]
}
],
"listingInfo":[
{
"bestOfferEnabled":[
"false"
],
"buyItNowAvailable":[
"false"
],
"startTime":[
"2014-10-13T09:36:12.000Z"
],
"endTime":[
"2015-03-12T09:41:12.000Z"
],
"listingType":[
"FixedPrice"
],
"gift":[
"false"
]
}
],
"galleryPlusPictureURL":[
"http:\/\/galleryplus.ebayimg.com\/ws\/web\/131321072184_1_5_1.jpg"
],
"condition":[
{
"conditionId":[
"1000"
],
"conditionDisplayName":[
"New with tags"
]
}
],
"isMultiVariationListing":[
"true"
],
"topRatedListing":[
"false"
]
},
答案 0 :(得分:2)
为什么需要JSONTokener
?这更容易:
JSONObject responseObject = new JSONObject(JSONResponse);
JSONArray main = responseObject.getJSONArray("findItemsAdvancedResponse");
JSONObject result = main.getJSONObject(0).getJSONArray("searchResult").getJSONObject(0);
JSONArray items = result.getJSONArray("item");
答案 1 :(得分:0)
您的nextValue()方法必须返回一个String,然后您尝试将其强制转换为JSONObject。我首先将语句拆分为较小的语句以帮助隔离问题。您正在实例化一个新对象,在其上调用一个方法,并在同一行上键入所有对象。这使得代码更难调试。
JSONTokener tokener = new JSONTokener(JSONResponse);
Object obj = tokener.nextValue();
if(obj.getClass().equals(JSONObject.class))
{
JSONObject responseObject = (JSONObject)obj;
}
else
{
System.out.println("Not a JSONObject");
}