上周我决定尝试使用Google购物API,但我不知道如何解析JCON对象。
在这里搜索了很多之后,我得到了一件商品的产品信息!然而,我不能把范围缩小到产品中的一个字符串。例如,我只想得到一个产品的标题。
我有以下内容:
jsonString:
{
"kind": "shopping#products",
"etag": ""GKsxsRlaBDslDpMe-MT1O7wqUDE/dMvQ5Pu2C806fWZJbNJ0GjdesJs"",
"id": "tag:google.com,2010:shopping/products",
"selfLink": "https://www.googleapis.com/shopping/search/v1/public/products?country=US&restrictBy=gtin:051500240908&startIndex=1&maxResults=25",
"totalItems": 3,
"startIndex": 1,
"itemsPerPage": 25,
"currentItemCount": 3,
"items": [
{
"kind": "shopping#product",
"id": "tag:google.com,2010:shopping/products/7585088/9884865157760252836",
"selfLink": "https://www.googleapis.com/shopping/search/v1/public/products/7585088/gid/9884865157760252836",
"product": {
"googleId": "9884865157760252836",
"author": {
"name": "Southeastern Delivery",
"accountId": "7585088"
},
"creationTime": "2011-07-25T00:15:58.000Z",
"modificationTime": "2012-02-11T09:29:00.000Z",
"country": "US",
"language": "en",
"title": "Jif Peanut Butter, Creamy",
"description": "Jif Creamy Peanut Butter. Fresh Roasted Peanut Taste. Look for the flavor seal. Contains no preservatives. No refrigeration required.",
"link": "http://www.southeasterndelivery.com/Jif_Peanut_Butter_00051500240908/",
"brand": "Jif Peanut Butter",
"condition": "new",
"gtin": "00051500240908",
"gtins": [
"00051500240908"
],
"inventories": [
{
"channel": "online",
"availability": "inStock",
"price": 15.64,
"shipping": 1.56,
"currency": "USD"
}
],
"images": [
{
"link": "http://www.southeasterndelivery.com/images/ProductImages/00051500240908.jpg"
}
]
}
},
],
"requestId": "0CLGzkcKVo64CFRDd5wod4mMAAA"
}
我在我的android应用程序中有以下代码来解析它:
try {
JSONObject jsonObject = new JSONObject(jsonString);
JSONArray itemsArray = jsonObject.getJSONArray("items");
JSONObject productObject = itemsArray.getJSONObject(0);
//String productTitle = productObject.getString("title");
//tv.setText(productTitle);
tv.setText(productObject.toString());
} catch (JSONException e) {
// TODO Auto-generated catch block
tv.setText("JSONOBJECT Error: " + e);
e.printStackTrace();
}
setContentView(tv);
我的Android应用程序上的TextView现在将显示(显然没有缩进,我这样做是为了便于阅读):
"product": {
"googleId": "9884865157760252836",
"author": {
"name": "Southeastern Delivery",
"accountId": "7585088"
},
"creationTime": "2011-07-25T00:15:58.000Z",
"modificationTime": "2012-02-11T09:29:00.000Z",
"country": "US",
"language": "en",
"title": "Jif Peanut Butter, Creamy",
"description": "Jif Creamy Peanut Butter. Fresh Roasted Peanut Taste. Look for the flavor seal. Contains no preservatives. No refrigeration required.",
"link": "http://www.southeasterndelivery.com/Jif_Peanut_Butter_00051500240908/",
"brand": "Jif Peanut Butter",
"condition": "new",
"gtin": "00051500240908",
"gtins": [
"00051500240908"
],
"inventories": [
{
"channel": "online",
"availability": "inStock",
"price": 15.64,
"shipping": 1.56,
"currency": "USD"
}
],
"images": [
{
"link": "http://www.southeasterndelivery.com/images/ProductImages/00051500240908.jpg"
}
]
}
现在,如果您注意到我的Java代码中有两行注释掉了。如果我取消注释这些行,然后注释该行:
tv.setText(productObject.toString());
tv被设置为这个错误:"JSONOBECT错误:org.json.JSONception:标题没有值"。我不知道为什么这是真的,因为它们显然是productObject中的一个标题。
任何帮助都会很棒!
我在项目中使用json智能。它很小,速度也很快。要将JSON从Sting转换为实际对象,请使用JSONObjet json = (JSONObject)JSONValue.parse(rawString);
当您拥有JSONObject时,您将其视为一个Map。因此String title = (String) json.get("title")
您的代码中缺少一定级别的信息。JSON数组包含项,其中包含products并具有标题。
你的代码应该看起来像:
JSONObject jsonObject = new JSONObject(jsonString);
JSONArray itemsArray = jsonObject.getJSONArray("items");
JSONObject itemObject = itemsArray.getJSONObject(0);
JSONObject productObject = itemObject.getJSONObject("product");
String productTitle = productObject.getString("title");