[英]Json to string in c#
此JSON数据http://api.geonames.org/citiesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo
我怎么能
XmlTextReader reader = new XmlTextReader("http://api.geonames.org/citiesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo");
TextBox1.Text = reader.tostring();
我对用于JSON的C#库感到满意。
然后,您可以编写如下代码
using System;
using System.IO;
using System.Net;
using System.Text;
using Newtonsoft.Json;
namespace Examples.System.Net
{
public class WebRequestGetExample
{
public static void Main ()
{
// Create a request for the URL.
WebRequest request = WebRequest.Create (
"http://api.geonames.org/citiesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo");
// Get the response.
WebResponse response = request.GetResponse ();
// Get the stream containing content returned by the server.
Stream dataStream = response.GetResponseStream ();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader (dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd ();
// Parse JSON
JObject o = JObject.Parse(responseFromServer);
JArray status= (JArray)o["status"];
string message = (string)status["message"];
// Clean up the streams and the response.
reader.Close ();
response.Close ();
}
}
}
var json = new WebClient().DownloadString("http://api.geonames.org/citiesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo");
TextBox1.Text = json;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.