Question

J'ai décidé que je voulais essayer d'utiliser l'API Google Shopping la semaine dernière, mais je ne savais pas comment analyser les objets JCON.

Après beaucoup de recherches ici, j'ai pu obtenir les informations sur le produit pour un article! Cependant, je ne peux pas m'arrêter à une chaîne dans le produit. Ainsi, par exemple, je veux simplement obtenir le titre d'un produit.

J'ai ce qui suit:

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"
 }

J'ai le code suivant dans mon application Android pour l'analyser:

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

Le TextView sur mon application Android s'affiche désormais (évidemment pas en retrait, je l'ai fait pour une lecture facile):

   "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"
     }
    ]
   }

Maintenant, si vous remarquez dans mon code Java, j'ai deux lignes commenté. Si je décalage ces lignes et commentez ensuite la ligne:

tv.setText(productObject.toString());

TV est défini sur cette erreur: "JSONObject Error: org.json.jsoneCection: aucune valeur pour le titre". Je ne sais pas pourquoi cela est vrai car il s'agit clairement d'un titre dans le produit.

Toute aide est la bienvenue!

Était-ce utile?

La solution

Vous manquez un niveau d'informations dans votre code. Le tableau JSON contient éléments, qui contient des produits, qui ont un Titre.

Votre code devrait ressembler:

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

Autres conseils

j'utilise json-smart Dans mon projet. Il est très petit et très rapide. Pour convertir votre JSON de Sting en usage réel de l'objet JSONObjet json = (JSONObject)JSONValue.parse(rawString);

Lorsque vous avez jsonObject, vous le traitez comme une carte. Alors String title = (String) json.get("title")

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top