Web Services - Leggere uno stream JSON tramite il componente JSON.NET

lunedì 13 ottobre 2008 - 18.11

andry82 Profilo | Newbie

Vorrei implementare la ricerca di Google (http://code.google.com/apis/ajaxsearch/documentation/#fonje) nel mio sito utilizzando i dati di ritorno codificati in stringa JSON (http://it.wikipedia.org/wiki/JSON) decodificati tramite il componente JSON.NET (http://www.codeplex.com/Json/Release/ProjectReleases.aspx?ReleaseId=16593)

Nel mio codice leggo i risultati tramite il seguente codice (l'url è di esempio):

Dim client As WebClient = New WebClient
Dim urlFormat As String = ""
If String.IsNullOrEmpty(urlFormat) Then
urlFormat = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=Paris%20Hilton"
End If
Dim url As Uri = New Uri(urlFormat)
Dim Search As String = client.DownloadString(url)

Cerco di decodificare la stinga ottenuta ma ottengo un errore:

Dim reader As JsonTextReader = New JsonTextReader(New StringReader(Search))
reader.Read()

Ottengo il seguente errore:
JSON root object has multiple properties. The root object must have a single property in order to create a valid XML document.

Come posso leggere i risultati?

Partendo dalla stringa JSON come questa:

{"responseData": {
"results": [
{
"GsearchResultClass": "GwebSearch",
"unescapedUrl": "http://en.wikipedia.org/wiki/Paris_Hilton",
"url": "http://en.wikipedia.org/wiki/Paris_Hilton",
"visibleUrl": "en.wikipedia.org",
"cacheUrl": "http://www.google.com/search?q\u003dcache:TwrPfhd22hYJ:en.wikipedia.org",
"title": "\u003cb\u003eParis Hilton\u003c/b\u003e - Wikipedia, the free encyclopedia",
"titleNoFormatting": "Paris Hilton - Wikipedia, the free encyclopedia",
"content": "\[1\] In 2006, she released her debut album..."
},
{
"GsearchResultClass": "GwebSearch",
"unescapedUrl": "http://www.imdb.com/name/nm0385296/",
"url": "http://www.imdb.com/name/nm0385296/",
"visibleUrl": "www.imdb.com",
"cacheUrl": "http://www.google.com/search?q\u003dcache:1i34KkqnsooJ:www.imdb.com",
"title": "\u003cb\u003eParis Hilton\u003c/b\u003e",
"titleNoFormatting": "Paris Hilton",
"content": "Self: Zoolander. Socialite \u003cb\u003eParis Hilton\u003c/b\u003e..."
},
...
],
"cursor": {
"pages": [
{ "start": "0", "label": 1 },
{ "start": "4", "label": 2 },
{ "start": "8", "label": 3 },
{ "start": "12","label": 4 }
],
"estimatedResultCount": "59600000",
"currentPageIndex": 0,
"moreResultsUrl": "http://www.google.com/search?oe\u003dutf8\u0026ie\u003dutf8..."
}
}
, "responseDetails": null, "responseStatus": 200}


Vorrei leggere i dati in questo modo:

Console.WriteLine(results(0).GsearchResultClass)
Console.WriteLine(results(0).unescapedUrl)
Console.WriteLine(results(0).title)
...
Console.WriteLine(results(1).GsearchResultClass)
Console.WriteLine(results(1).unescapedUrl)
Console.WriteLine(results(1).title)



E' possibile ottenere questo risultato?

Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?

Dopo esserti registrato potrai chiedere
aiuto sul nostro Forum oppure aiutare gli altri

Consulta le Stanze disponibili.

Registrati ora !
Copyright © dotNetHell.it 2002-2024
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5