C# 尝试使用JSON.net接收JSON
我已经开始构建一个应用程序,从世界天气在线获取潮汐信息、天气信息、地图功能等,但它无法获得JSON响应 我已经通过NuGet安装了JSON.net,我正在运行Windows 8.1。我甚至只能通过将JSON URL粘贴到实际浏览器中来查看数据。然后使用json2sharp的输出创建下面代码的底部 试过又试过C# 尝试使用JSON.net接收JSON,c#,json,weather,c3,C#,Json,Weather,C3,我已经开始构建一个应用程序,从世界天气在线获取潮汐信息、天气信息、地图功能等,但它无法获得JSON响应 我已经通过NuGet安装了JSON.net,我正在运行Windows 8.1。我甚至只能通过将JSON URL粘贴到实际浏览器中来查看数据。然后使用json2sharp的输出创建下面代码的底部 试过又试过 使用系统; Net系统; 使用System.Collections.Generic; 使用系统组件模型; 使用系统数据; 使用系统图; 使用System.Linq; 使用系统文本; 使用Sy
使用系统;
Net系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用GMap.NET.WindowsForms;
使用GMap.NET;
使用GMap.NET.MapProviders;
使用System.Xml.Linq;
使用System.Xml;
使用System.IO;
使用System.Web;
使用Newtonsoft.Json.Linq;
使用Newtonsoft.Json;
使用RestSharp;
使用Newtonsoft.Json.Utilities;
名称空间EOD_助手
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
私有void myMap_加载(对象发送方,事件参数e)
{
myMap.MapProvider=BingHybridMapProvider.Instance;
myMap.SetPositionByKeywords(“Comox,加拿大”);
myMap.MinZoom=1;
myMap.MaxZoom=17;
myMap.Zoom=10;
myMap.candrampap=true;
myMap.MapScaleInfoEnabled=true;
}
私有void Form1\u加载(对象发送方、事件参数e)
{
字符串url=”http://api.worldweatheronline.com/free/v2/weather.ashx?q=Campbell%20River&format=json&num_of_days=5&callback=rrr&key=1ccf49ff57a386e570286ae9294b9";
}
二等兵
{
}
私有void weatherBtn_单击(对象发送方,事件参数e)
{
}
公共级天气预报
{
公共字符串值{get;set;}
}
公共级天气预报器
{
公共字符串值{get;set;}
}
公共类当前状态
{
公共字符串cloudcover{get;set;}
公共字符串FeelsLikeC{get;set;}
公共字符串FeelsLikeF{get;set;}
公共字符串{get;set;}
公共字符串观察时间{get;set;}
公共字符串precipMM{get;set;}
公用管柱压力{get;set;}
公共字符串temp_C{get;set;}
公共字符串temp_F{get;set;}
公共字符串可见性{get;set;}
公共字符串天气代码{get;set;}
公共列表weatherDesc{get;set;}
公共列表weatherIconUrl{get;set;}
公共字符串winddir16Point{get;set;}
公共字符串winddirDegree{get;set;}
公共字符串windspeedKmph{get;set;}
公共字符串windspeedMiles{get;set;}
}
公共类请求
{
公共字符串查询{get;set;}
公共字符串类型{get;set;}
}
公共天文课
{
公共字符串moonrise{get;set;}
公共字符串moonset{get;set;}
公共字符串sunrise{get;set;}
公共字符串{get;set;}
}
公共类天气预报2
{
公共字符串值{get;set;}
}
公共类天气预报器
{
公共字符串值{get;set;}
}
公共课每小时
{
公共字符串chanceoffog{get;set;}
公共字符串chanceoffrost{get;set;}
公共字符串chanceofhightemp{get;set;}
公共字符串chanceofovercast{get;set;}
公共字符串chanceofrain{get;set;}
公共字符串chanceofremdry{get;set;}
公共字符串chanceofsnow{get;set;}
公共字符串chanceofsunshine{get;set;}
公共字符串chanceofthunder{get;set;}
公共字符串chanceofwindy{get;set;}
公共字符串cloudcover{get;set;}
公共字符串DewPointC{get;set;}
公共字符串DewPointF{get;set;}
公共字符串FeelsLikeC{get;set;}
公共字符串FeelsLikeF{get;set;}
公共字符串HeatIndexC{get;set;}
公共字符串HeatIndexF{get;set;}
公共字符串{get;set;}
公共字符串precipMM{get;set;}
公用管柱压力{get;set;}
公共字符串tempC{get;set;}
公共字符串tempF{get;set;}
公共字符串时间{get;set;}
公共字符串可见性{get;set;}
公共字符串天气代码{get;set;}
公共列表weatherDesc{get;set;}
公共列表weatherIconUrl{get;set;}
公共字符串WindChillC{get;set;}
公共字符串WindChillF{get;set;}
公共字符串winddir16Point{get;set;}
公共字符串winddirDegree{get;set;}
公共字符串WindGustKmph{get;set;}
公共字符串{get;set;}
公共字符串windspeedKmph{get;set;}
公共字符串windspeedMiles{get;set;}
}
公共天气
{
公共列表{get;set;}
公共字符串日期{get;set;}
公共列表{get;set;}
公共字符串maxtempC{get;set;}
公共字符串maxtempF{get;set;}
公共字符串mintempC{get;set;}
公共字符串mintempF{get;set;}
公共字符串索引{get;set;}
}
公共类数据
{
公共列表当前_条件{get;set;}
公开名单要求
using System;
using System.Net;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using GMap.NET.WindowsForms;
using GMap.NET;
using GMap.NET.MapProviders;
using System.Xml.Linq;
using System.Xml;
using System.IO;
using System.Web;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using RestSharp;
using Newtonsoft.Json.Utilities;
namespace EOD_Assistant
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void myMap_Load(object sender, EventArgs e)
{
myMap.MapProvider = BingHybridMapProvider.Instance;
myMap.SetPositionByKeywords("Comox, Canada");
myMap.MinZoom = 1;
myMap.MaxZoom = 17;
myMap.Zoom = 10;
myMap.CanDragMap = true;
myMap.MapScaleInfoEnabled = true;
}
private void Form1_Load(object sender, EventArgs e)
{
string url = "http://api.worldweatheronline.com/free/v2/weather.ashx?q=Campbell%20River&format=json&num_of_days=5&callback=rrr&key=1ccf49ff57a386e570286ae9294b9";
}
private void GetWeather()
{
}
private void weatherBtn_Click(object sender, EventArgs e)
{
}
public class WeatherDesc
{
public string value { get; set; }
}
public class WeatherIconUrl
{
public string value { get; set; }
}
public class CurrentCondition
{
public string cloudcover { get; set; }
public string FeelsLikeC { get; set; }
public string FeelsLikeF { get; set; }
public string humidity { get; set; }
public string observation_time { get; set; }
public string precipMM { get; set; }
public string pressure { get; set; }
public string temp_C { get; set; }
public string temp_F { get; set; }
public string visibility { get; set; }
public string weatherCode { get; set; }
public List<WeatherDesc> weatherDesc { get; set; }
public List<WeatherIconUrl> weatherIconUrl { get; set; }
public string winddir16Point { get; set; }
public string winddirDegree { get; set; }
public string windspeedKmph { get; set; }
public string windspeedMiles { get; set; }
}
public class Request
{
public string query { get; set; }
public string type { get; set; }
}
public class Astronomy
{
public string moonrise { get; set; }
public string moonset { get; set; }
public string sunrise { get; set; }
public string sunset { get; set; }
}
public class WeatherDesc2
{
public string value { get; set; }
}
public class WeatherIconUrl2
{
public string value { get; set; }
}
public class Hourly
{
public string chanceoffog { get; set; }
public string chanceoffrost { get; set; }
public string chanceofhightemp { get; set; }
public string chanceofovercast { get; set; }
public string chanceofrain { get; set; }
public string chanceofremdry { get; set; }
public string chanceofsnow { get; set; }
public string chanceofsunshine { get; set; }
public string chanceofthunder { get; set; }
public string chanceofwindy { get; set; }
public string cloudcover { get; set; }
public string DewPointC { get; set; }
public string DewPointF { get; set; }
public string FeelsLikeC { get; set; }
public string FeelsLikeF { get; set; }
public string HeatIndexC { get; set; }
public string HeatIndexF { get; set; }
public string humidity { get; set; }
public string precipMM { get; set; }
public string pressure { get; set; }
public string tempC { get; set; }
public string tempF { get; set; }
public string time { get; set; }
public string visibility { get; set; }
public string weatherCode { get; set; }
public List<WeatherDesc2> weatherDesc { get; set; }
public List<WeatherIconUrl2> weatherIconUrl { get; set; }
public string WindChillC { get; set; }
public string WindChillF { get; set; }
public string winddir16Point { get; set; }
public string winddirDegree { get; set; }
public string WindGustKmph { get; set; }
public string WindGustMiles { get; set; }
public string windspeedKmph { get; set; }
public string windspeedMiles { get; set; }
}
public class Weather
{
public List<Astronomy> astronomy { get; set; }
public string date { get; set; }
public List<Hourly> hourly { get; set; }
public string maxtempC { get; set; }
public string maxtempF { get; set; }
public string mintempC { get; set; }
public string mintempF { get; set; }
public string uvIndex { get; set; }
}
public class Data
{
public List<CurrentCondition> current_condition { get; set; }
public List<Request> request { get; set; }
public List<Weather> weather { get; set; }
}
public class RootObject
{
public Data data { get; set; }
}
public class PleaseWORK
{
private static T _download_serialized_json_data<T>(string url) where T : new()
{
using (var w = new WebClient())
{
var json_data = string.Empty;
// attempt to download JSON data as a string
try
{
json_data = w.DownloadString(url);
}
catch (Exception) { }
// if string with JSON data is not empty, deserialize it to class and return its instance
return !string.IsNullOrEmpty(json_data) ? JsonConvert.DeserializeObject<T>(json_data) : new T();
}
}
}
}
}
string url = "http://api.worldweatheronline.com/free/v2/weather.ashx?q=Campbell%20River&format=json&num_of_days=5&callback=rrr&key=xxxxxxxxxxxxxxxxxxxxxxx";
using (var webClient = new System.Net.WebClient())
{
var json = webClient.DownloadString(url); //THIS GIVES ME AN EXCEPTION ERROR....DUNNO WHY.
}
string url = "http://api.worldweatheronline.com/free/v2/weather.ashx?q=Campbell%20River&format=json&num_of_days=5&callback=rrr&key=1ccf49ff57a386e570286ae9294b9";
var jsonString = web.DownloadString(url).ToString();
var parseJsonString = jsonString.Split('(', ')')[1];
var obj = JsonConvert.DeserializeObject<RootObject>(parseJsonString);
// httpWebRequest with API URL
HttpWebRequest request = (HttpWebRequest)WebRequest.Create
("http://YourURL.com");
//Method GET
request.Method = "GET";
//HttpWebResponse for result
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
//Mapping of status code
if (response.StatusCode == HttpStatusCode.OK)
{
Stream receiveStream = response.GetResponseStream();
StreamReader readStream = null;
if (response.CharacterSet == "")
readStream = new StreamReader(receiveStream);
else
readStream = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet));
//Get news data in json string
string data = readStream.ReadToEnd();
}
DataSet ds = new DataSet();
StringReader reader = new StringReader(data);
ds.ReadXml(reader);
http://api.worldweatheronline.com/free/v2/weather.ashx?q=Campbell%20River&format=json&num_of_days=5&key=xxxxxxxxxxxxxxxxxxxxx