質問

先週、Google Shopping 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コードに気づいた場合、2行がコメントアウトされています。私がそれらの行を外してから、行にコメントする場合:

tv.setText(productObject.toString());

テレビはこのエラーに設定されます:「jsonobjectエラー:org.json.jsoneception:タイトルの値はありません」。明らかに彼らがProductObjectのタイトルであるため、なぜこれが真実かはわかりません。

どんな助けも素晴らしいでしょう!

役に立ちましたか?

解決

コードのレベルの情報がありません。 JSONアレイには含まれています アイテム, 、 を含む 製品, 、aを持っています 題名.

あなたのコードは次のように見えるはずです:

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");

他のヒント

私は使用しています json-smart 私のプロジェクトで。それは非常に小さく、非常に速いです。 JSONを刺しから実際のオブジェクト使用に変換するには JSONObjet json = (JSONObject)JSONValue.parse(rawString);

JSonObjectを持っていると、マップのように扱います。そう String title = (String) json.get("title")

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top