Domanda

Ho deciso che volevo provare a utilizzare l'API di shopping di Google la scorsa settimana, ma non avevo idea di come analizzare gli oggetti JCON.

Dopo molte ricerche qui sono stato in grado di ottenere le informazioni sul prodotto per un articolo! Tuttavia, non posso restringere solo una stringa nel prodotto. Quindi, ad esempio, voglio solo ottenere il titolo di un prodotto.

Ho quanto segue:

jostring:

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

Ho il seguente codice nella mia app Android per analizzarlo:

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

Il TextView sulla mia app Android ora verrà visualizzata (ovviamente non rientrata l'ho fatto per una lettura 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"
     }
    ]
   }

Ora se noti nel mio codice Java, ho commentato due righe. Se rimboro quelle righe e poi commento la riga:

tv.setText(productObject.toString());

La TV viene impostata su questo errore: "Errore JSONObject: org.json.jsoneception: nessun valore per il titolo". Non sono sicuro del perché questo sia vero perché chiaramente il loro è un titolo in ProductObject.

Qualsiasi aiuto sarebbe grande!

È stato utile?

Soluzione

Ti manca un livello di informazioni nel tuo codice. L'array JSON contiene Oggetti, che contiene prodotti, che hanno a titolo.

Il tuo codice dovrebbe apparire:

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

Altri suggerimenti

sto usando Json-Smart nel mio progetto. È molto piccolo e molto veloce. Per convertire il tuo JSON da Sting all'uso di oggetti reali JSONObjet json = (JSONObject)JSONValue.parse(rawString);

Quando hai JSONObject lo tratti proprio come una mappa. Così String title = (String) json.get("title")

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top