Domanda

Sono molto nuovo a JSON, delle API di Google. Quindi, per favore guida.
Sto cercando di scrivere un'applicazione in 'Java' che avrebbe utilizzato Google Custom serch api attraverso riposante. Ho iniziato a imparare JSON e passando attraverso [link] http://code.google.com /apis/customsearch/v1/overview.html volevo scrivere del codice.

Qui viene presentata l'JSON di ricerca di google:
http://code.google.com/apis/customsearch/v1/reference .html # method_search_cse_list

http://code.google.com/apis/customsearch/ v1 / reference.html

Dal riferimento ho trovato che campi di questa CustomSearch sarebbero stringa o int o qualsiasi altro tipo di dati. Essi hanno anche definito la struttura di ogni oggetto.

Ma io sto affrontando problemi con alcuni tipi di dati:

items.title     array   The title of the search result, in plain text.
items.snippet   array   The snippet of the search result, in plain text.
items.pagemap   object  Contains pagemap information for this search result.    
items.pagemap.value     array   Pagemap information, keyed by the name of this pagemap.     
items.pagemap.value.value   object  The actual pagemap information.

Come avrei li definisco nella mia classe. che tipo di array è stringa del titolo o char e questo PageMap è una certa convenzione o di qualsiasi sito può dare i propri tag.

// classe CustomSearch

public class CustomSearch {
public URL getURL() throws MalformedURLException{
    return url.getURL();
}

@Key ("items") ArrayList<SearchResult> results;
private @Key SearchURL url;
private @Key Query queries; 

}

// classe

class SearchResult {
public SearchResult(){        
}

public String getTitle(){
    return title;
}
public String getLink(){
    return link;
}
public String getSnippet(){
    return snippet;
}

private @Key String title;   // is this right ?
private @Key String htmlTitle;
private @Key String link;
private @Key String snippet;   // is this right ?
private @Key String htmlSnippet;    

}

È stato utile?

Soluzione

Ho emesso una ricerca reale utilizzando la mia chiave come suggerito nell'esempio di Google:

GET https://www.googleapis.com/customsearch/v1?key=INSERT-YOUR-KEY&cx=013036536707430787589:_pqjad5hr1a&q=flowers&alt=json

E qui è ciò che ottengo (che mostra solo bit di dati)

items": [
  {
   "kind": "customsearch#result",
   "title": "FTD.COM - Flowers Online | Roses, Fresh Flowers, Plants and Gift ...",
   "htmlTitle": "FTD.COM - \u003cb\u003eFlowers\u003c/b\u003e Online | Roses, Fresh \u003cb\u003eFlowers\u003c/b\u003e, Plants and Gift \u003cb\u003e...\u003c/b\u003e",
   "link": "http://www.ftd.com/",
   "displayLink": "www.ftd.com",
   "snippet": "Aug 2, 2011 ... Order flowers online for same day floral delivery. Shop for flowers, chocolates,   roses, gifts and gift baskets by occasion, season or get beautiful ...",
   "htmlSnippet": "Aug 2, 2011 \u003cb\u003e...\u003c/b\u003e Order \u003cb\u003eflowers\u003c/b\u003e online for same day floral delivery. Shop for \u003cb\u003eflowers\u003c/b\u003e, chocolates, \u003cbr\u003e  roses, gifts and gift baskets by occasion, season or get beautiful \u003cb\u003e...\u003c/b\u003e",
   "cacheId": "D_MQAIEeVpAJ",
   "pagemap": {
    "metatags": [
     {
      "y_key": "e887dc108fef83f6",
      "msvalidate.01": "71957E1C9D33211154243270EB14C63C"
     }
    ]
   }
  ......

Si presenta come:

items.title     array   The title of the search result, in plain text.

Questo appare come un tipo di dati String dal mucchio di risultati che ottengo, quindi non so perché il riferimento classificato come array

items.snippet   array   The snippet of the search result, in plain text.

Questo appare come un tipo di dati String e dai risultati che ho ottenuto

items.pagemap   object  Contains pagemap information for this search result.    
items.pagemap.value     array   Pagemap information, keyed by the name of this pagemap.     
items.pagemap.value.value   object  The actual pagemap information.

In base alla PageMap descrizione Questo appare come un arbitrario valore-chiave di dati coppia che il sito potrebbe fornire.

Di seguito sono riportati alcuni dei PageMap che ricevo dal mio banco di prova per il vostro riferimento:

 "pagemap": {
    "metatags": [
     {
      "y_key": "e887dc108fef83f6",
      "msvalidate.01": "71957E1C9D33211154243270EB14C63C"
     }
    ]
   }

 "pagemap": {
    "website": [
     {
      "type": "website",
      "title": "ProFlowers",
      "description": "The freshest flowers, guaranteed to last at least 7 days.",
      "image": "http://a1128.g.akamai.net/7/1128/497/0001/images.proflowers.com/pcsite/ProflowersLogo_nb.gif",
      "url": "http://www.proflowers.com/",
      "site_name": "ProFlowers",
      "app_id": "180475245301608"
     }
    ],
    "metatags": [
     {
      "msnbot": "NOODP",
      "msvalidate.01": "77940E049C181974C3AA656C72688B4C"
     }
    ]
   }

  "pagemap": {
    "metatags": [
     {
      "viewport": "width=device-width; initial-scale=1.0; maximum-scale=1.0;"
     }
    ]

Come PageMap è molto destrutturato vorrei memorizzarli come Map<String, JSONObject> pagemap. Come si può vedere che ho appena mantenere l'originale JSONObject in PageMap così nel caso in cui se ne avete bisogno, si può sempre estrarlo. A meno che non v'è un insieme di definizioni che tipo si potrebbe mettere in PageMap insieme con i suoi campi, che rappresenta il valore del PageMap come classe potrebbe essere difficile.

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