Cómo obtener una cadena json de url?
Estoy cambiando mi formulario de código XML a JSON.
Pero no puedo encontrar cómo obtener una cadena JSON de una url dada.
La URL es algo así: " https://api.facebook.com/method/fql.query?query=.....&format=json "
Usé XDocuments antes, allí podría usar el método de carga:
XDocument doc = XDocument.load("URL");
¿Cuál es el equivalente de este método para JSON? Estoy usando JSON.NET.
121
3 answers
Use la clase WebClient
en System.Net
:
var json = new WebClient().DownloadString("url");
Tenga en cuenta que WebClient
es IDisposable
, por lo que probablemente agregaría una instrucción using
a esto en el código de producción. Esto se vería como:
using (WebClient wc = new WebClient())
{
var json = wc.DownloadString("url");
}
225
Author: Zebi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-08-03 19:20:21
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-08-03 19:20:21
AFAIK JSON.Net no proporciona funcionalidad para leer desde una URL. Así que necesitas hacer esto en dos pasos:
using (var webClient = new System.Net.WebClient()) {
var json = webClient.DownloadString(URL);
// Now parse with JSON.Net
}
92
Author: Jon,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-04-06 13:17:47
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-04-06 13:17:47
Si está usando. NET 4.5 y desea usar async, puede usar HttpClient
en System.Net.Http
:
using (var httpClient = new HttpClient())
{
var json = await httpClient.GetStringAsync("url");
// Now parse with JSON.Net
}
37
Author: Richard Garside,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-09-17 15:14:43
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-09-17 15:14:43