Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用json.net反序列化大块json数据_C#_Json_Json.net - Fatal编程技术网

C# 使用json.net反序列化大块json数据

C# 使用json.net反序列化大块json数据,c#,json,json.net,C#,Json,Json.net,我正在慢慢学习JSON响应以及如何使用JSON.net处理它们 我获得的数据可以在这里找到 例如,我试图从json的“通知”部分提取“版权”信息 我可以成功地下拉数据,但我不确定它是否被正确反序列化。我定义的文本框中没有显示代码中的数据,但数据正在向下拉,因为我可以在ode中定义的第二个文本框中显示原始响应 你能告诉我我做错了什么吗 谢谢:) 使用系统; 使用System.Collections.Generic; 使用系统组件模型; 使用系统数据; 使用系统图; 使用System.Linq;

我正在慢慢学习JSON响应以及如何使用JSON.net处理它们

我获得的数据可以在这里找到

例如,我试图从json的“通知”部分提取“版权”信息

我可以成功地下拉数据,但我不确定它是否被正确反序列化。我定义的文本框中没有显示代码中的数据,但数据正在向下拉,因为我可以在ode中定义的第二个文本框中显示原始响应

你能告诉我我做错了什么吗

谢谢:)

使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
使用System.IO;
Net系统;
使用System.Runtime.Serialization.Json;
使用System.Runtime.Serialization;
使用System.Web;
使用Newtonsoft.Json.Linq;
命名空间RESTTEST2
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
公开课公告
{
公共字符串版权{get;set;}
公共字符串版权\u url{get;set;}
公共字符串免责声明\u url{get;set;}
公共字符串反馈_url{get;set;}
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
richTextBox1.Text=“测试”;
字符串url=”http://www.bom.gov.au/fwo/IDN60901/IDN60901.95764.json";
HttpWebRequest req=WebRequest.Create(url)
作为HttpWebRequest;
请求凭证=CredentialCache.DefaultCredentials;
req.ContentType=“应用程序/json”;
字符串结果=null;
使用(HttpWebResponse resp=req.GetResponse()
作为HttpWebResponse)
{
流阅读器=
新的StreamReader(resp.GetResponseStream());
结果=reader.ReadToEnd();
richTextBox2.Text=结果;
var bar=Newtonsoft.Json.JsonConvert.DeserializeObject(结果);
var bar2=bar.copyright\u url;
var bar3=result.ToString();
richTextBox1.Text=bar2;
}
}
}
}
下面是一个正在工作的dotNetFiddle解决方案。 单击fiddle页面上的Run并查看控制台输出

解决方案详细信息
注意
是一个JSON数组/C#
列表
位于
观察
根对象内

因此,像下面这样声明类,并使用
jsonvert。在
RootObject
上反序列化
,然后从
观察
RootObjects访问
通知
列表

我曾经从JSON字符串生成C#类

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Runtime.Serialization.Json;
using System.Runtime.Serialization;
using System.Web;
using Newtonsoft.Json.Linq;



namespace RESTTEST2
{
 public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

public class Notice
{
    public string copyright { get; set; }
    public string copyright_url { get; set; }
    public string disclaimer_url { get; set; }
    public string feedback_url { get; set; }
}

public class Header
{
    public string refresh_message { get; set; }
    public string ID { get; set; }
    public string main_ID { get; set; }
    public string name { get; set; }
    public string state_time_zone { get; set; }
    public string time_zone { get; set; }
    public string product_name { get; set; }
    public string state { get; set; }
}

public class Datum
{
    public int sort_order { get; set; }
    public int wmo { get; set; }
    public string name { get; set; }
    public string history_product { get; set; }
    public string local_date_time { get; set; }
    public string local_date_time_full { get; set; }
    public string aifstime_utc { get; set; }
    public double air_temp { get; set; }
    public double apparent_t { get; set; }
    public string cloud { get; set; }
    public object cloud_base_m { get; set; }
    public int cloud_oktas { get; set; }
    public string cloud_type { get; set; }
    public object cloud_type_id { get; set; }
    public double delta_t { get; set; }
    public double dewpt { get; set; }
    public object gust_kmh { get; set; }
    public object gust_kt { get; set; }
    public double lat { get; set; }
    public double lon { get; set; }
    public object press { get; set; }
    public object press_msl { get; set; }
    public object press_qnh { get; set; }
    public string press_tend { get; set; }
    public string rain_trace { get; set; }
    public int rel_hum { get; set; }
    public string sea_state { get; set; }
    public string swell_dir_worded { get; set; }
    public object swell_height { get; set; }
    public object swell_period { get; set; }
    public string vis_km { get; set; }
    public string weather { get; set; }
    public string wind_dir { get; set; }
    public int wind_spd_kmh { get; set; }
    public int wind_spd_kt { get; set; }
}

public class Observations
{
    public List<Notice> notice { get; set; }
    public List<Header> header { get; set; }
    public List<Datum> data { get; set; }
}

public class RootObject
{
    public Observations observations { get; set; }
}

    private void button1_Click(object sender, EventArgs e)
    {
        richTextBox1.Text = "test";
        string url = "http://www.bom.gov.au/fwo/IDN60901/IDN60901.95764.json";

  HttpWebRequest req = WebRequest.Create(url)
                   as HttpWebRequest;
  req.Credentials = CredentialCache.DefaultCredentials;
  req.ContentType = "application/json";

  string result = null;
  using (HttpWebResponse resp = req.GetResponse()
                            as HttpWebResponse)
  {
  StreamReader reader =
  new StreamReader(resp.GetResponseStream());
  result = reader.ReadToEnd();
  richTextBox2.Text = result; 

  var bar = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(result);
  var bar2 = bar.observations.notice; // this will have the List<notice> 
  // now you can loop through the List<notice> in bar2 and do whatever you want with it's data.
  // var bar3 = result.ToString();
  richTextBox1.Text = bar2;

  }



  }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
使用System.IO;
Net系统;
使用System.Runtime.Serialization.Json;
使用System.Runtime.Serialization;
使用System.Web;
使用Newtonsoft.Json.Linq;
命名空间RESTTEST2
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
公开课公告
{
公共字符串版权{get;set;}
公共字符串版权\u url{get;set;}
公共字符串免责声明\u url{get;set;}
公共字符串反馈_url{get;set;}
}
公共类标题
{
公共字符串刷新\u消息{get;set;}
公共字符串ID{get;set;}
公共字符串main_ID{get;set;}
公共字符串名称{get;set;}
公共字符串状态\时区{get;set;}
公共字符串时区{get;set;}
公共字符串产品名称{get;set;}
公共字符串状态{get;set;}
}
公共类数据
{
公共整数排序顺序{get;set;}
公共int wmo{get;set;}
公共字符串名称{get;set;}
公共字符串历史_乘积{get;set;}
公共字符串local_date_time{get;set;}
公共字符串local_date_time_full{get;set;}
公共字符串aifstime_utc{get;set;}
公共双空气温度{get;set;}
公共双表观{get;set;}
公共字符串云{get;set;}
公共对象cloud\u base\u m{get;set;}
公共int-cloud_-oktas{get;set;}
公共字符串cloud_type{get;set;}
公共对象云\u类型\u id{get;set;}
公共双增量{get;set;}
公共双dewpt{get;set;}
公共对象gust_kmh{get;set;}
公共对象gust_kt{get;set;}
公共双lat{get;set;}
公共双lon{get;set;}
公共对象按{get;set;}
公共对象按{get;set;}
公共对象按{get;set;}
公共字符串按{get;set;}
公共字符串rain_trace{get;set;}
公共int rel_hum{get;set;}
公共字符串sea_state{get;set;}
公共字符串{get;set;}
公共对象膨胀高度{get;set;}
公共对象周期{get;set;}
公共字符串vis_km{get;set;}
公共字符串weather{get;set;}
公共字符串wind_dir{get;set;}
公共int wind spd kmh{get;set;}
公共int wind spd kt{get;set;}
}
公开课观察
{
公共列表通知{get;set;}
公共列表头{get;set;}
公共列表数据{get;set;}
}
公共类根对象
{
公共观察{get;set;}
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
richTextBox1.Text=“测试”;
字符串url=”http://www.bom.gov.au/fwo/IDN60901/IDN60901.95764.json";
HttpWebRequest req=WebRequest.Create(url)
作为HttpWebRequest;
请求凭证=CredentialCache.DefaultCredentials;
req.ContentType=“应用程序/json”;
字符串结果=null;
使用(HttpWebResponse resp=req.GetResponse()
作为HttpWebResponse)
{
流阅读器=
新的StreamReader(响应GetResponseStream(
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Runtime.Serialization.Json;
using System.Runtime.Serialization;
using System.Web;
using Newtonsoft.Json.Linq;



namespace RESTTEST2
{
 public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

public class Notice
{
    public string copyright { get; set; }
    public string copyright_url { get; set; }
    public string disclaimer_url { get; set; }
    public string feedback_url { get; set; }
}

public class Header
{
    public string refresh_message { get; set; }
    public string ID { get; set; }
    public string main_ID { get; set; }
    public string name { get; set; }
    public string state_time_zone { get; set; }
    public string time_zone { get; set; }
    public string product_name { get; set; }
    public string state { get; set; }
}

public class Datum
{
    public int sort_order { get; set; }
    public int wmo { get; set; }
    public string name { get; set; }
    public string history_product { get; set; }
    public string local_date_time { get; set; }
    public string local_date_time_full { get; set; }
    public string aifstime_utc { get; set; }
    public double air_temp { get; set; }
    public double apparent_t { get; set; }
    public string cloud { get; set; }
    public object cloud_base_m { get; set; }
    public int cloud_oktas { get; set; }
    public string cloud_type { get; set; }
    public object cloud_type_id { get; set; }
    public double delta_t { get; set; }
    public double dewpt { get; set; }
    public object gust_kmh { get; set; }
    public object gust_kt { get; set; }
    public double lat { get; set; }
    public double lon { get; set; }
    public object press { get; set; }
    public object press_msl { get; set; }
    public object press_qnh { get; set; }
    public string press_tend { get; set; }
    public string rain_trace { get; set; }
    public int rel_hum { get; set; }
    public string sea_state { get; set; }
    public string swell_dir_worded { get; set; }
    public object swell_height { get; set; }
    public object swell_period { get; set; }
    public string vis_km { get; set; }
    public string weather { get; set; }
    public string wind_dir { get; set; }
    public int wind_spd_kmh { get; set; }
    public int wind_spd_kt { get; set; }
}

public class Observations
{
    public List<Notice> notice { get; set; }
    public List<Header> header { get; set; }
    public List<Datum> data { get; set; }
}

public class RootObject
{
    public Observations observations { get; set; }
}

    private void button1_Click(object sender, EventArgs e)
    {
        richTextBox1.Text = "test";
        string url = "http://www.bom.gov.au/fwo/IDN60901/IDN60901.95764.json";

  HttpWebRequest req = WebRequest.Create(url)
                   as HttpWebRequest;
  req.Credentials = CredentialCache.DefaultCredentials;
  req.ContentType = "application/json";

  string result = null;
  using (HttpWebResponse resp = req.GetResponse()
                            as HttpWebResponse)
  {
  StreamReader reader =
  new StreamReader(resp.GetResponseStream());
  result = reader.ReadToEnd();
  richTextBox2.Text = result; 

  var bar = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(result);
  var bar2 = bar.observations.notice; // this will have the List<notice> 
  // now you can loop through the List<notice> in bar2 and do whatever you want with it's data.
  // var bar3 = result.ToString();
  richTextBox1.Text = bar2;

  }



  }
    }
}
using System;
using System.Net;
using System.Windows.Forms;
using Newtonsoft.Json;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1() {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e) {
            string url = @"http://www.bom.gov.au/fwo/IDN60901/IDN60901.95764.json";
            var client = new WebClient();
            var s = client.DownloadString(url);
            var deserializeObject = JsonConvert.DeserializeObject<SampleResponse1>(s);
        }
    }
}
using Newtonsoft.Json;

namespace WindowsFormsApplication2
{
    internal class Notice
    {
        [JsonProperty("copyright")]
        public string Copyright { get; set; }

        [JsonProperty("copyright_url")]
        public string CopyrightUrl { get; set; }

        [JsonProperty("disclaimer_url")]
        public string DisclaimerUrl { get; set; }

        [JsonProperty("feedback_url")]
        public string FeedbackUrl { get; set; }
    }

    internal class Header
    {
        [JsonProperty("refresh_message")]
        public string RefreshMessage { get; set; }

        [JsonProperty("ID")]
        public string ID { get; set; }

        [JsonProperty("main_ID")]
        public string MainID { get; set; }

        [JsonProperty("name")]
        public string Name { get; set; }

        [JsonProperty("state_time_zone")]
        public string StateTimeZone { get; set; }

        [JsonProperty("time_zone")]
        public string TimeZone { get; set; }

        [JsonProperty("product_name")]
        public string ProductName { get; set; }

        [JsonProperty("state")]
        public string State { get; set; }
    }

    internal class Datum
    {
        [JsonProperty("sort_order")]
        public int SortOrder { get; set; }

        [JsonProperty("wmo")]
        public int Wmo { get; set; }

        [JsonProperty("name")]
        public string Name { get; set; }

        [JsonProperty("history_product")]
        public string HistoryProduct { get; set; }

        [JsonProperty("local_date_time")]
        public string LocalDateTime { get; set; }

        [JsonProperty("local_date_time_full")]
        public string LocalDateTimeFull { get; set; }

        [JsonProperty("aifstime_utc")]
        public string AifstimeUtc { get; set; }

        [JsonProperty("air_temp")]
        public double AirTemp { get; set; }

        [JsonProperty("apparent_t")]
        public double ApparentT { get; set; }

        [JsonProperty("cloud")]
        public string Cloud { get; set; }

        [JsonProperty("cloud_base_m")]
        public object CloudBaseM { get; set; }

        [JsonProperty("cloud_oktas")]
        public int CloudOktas { get; set; }

        [JsonProperty("cloud_type")]
        public string CloudType { get; set; }

        [JsonProperty("cloud_type_id")]
        public object CloudTypeId { get; set; }

        [JsonProperty("delta_t")]
        public double DeltaT { get; set; }

        [JsonProperty("dewpt")]
        public double Dewpt { get; set; }

        [JsonProperty("gust_kmh")]
        public object GustKmh { get; set; }

        [JsonProperty("gust_kt")]
        public object GustKt { get; set; }

        [JsonProperty("lat")]
        public double Lat { get; set; }

        [JsonProperty("lon")]
        public double Lon { get; set; }

        [JsonProperty("press")]
        public object Press { get; set; }

        [JsonProperty("press_msl")]
        public object PressMsl { get; set; }

        [JsonProperty("press_qnh")]
        public object PressQnh { get; set; }

        [JsonProperty("press_tend")]
        public string PressTend { get; set; }

        [JsonProperty("rain_trace")]
        public string RainTrace { get; set; }

        [JsonProperty("rel_hum")]
        public int RelHum { get; set; }

        [JsonProperty("sea_state")]
        public string SeaState { get; set; }

        [JsonProperty("swell_dir_worded")]
        public string SwellDirWorded { get; set; }

        [JsonProperty("swell_height")]
        public object SwellHeight { get; set; }

        [JsonProperty("swell_period")]
        public object SwellPeriod { get; set; }

        [JsonProperty("vis_km")]
        public string VisKm { get; set; }

        [JsonProperty("weather")]
        public string Weather { get; set; }

        [JsonProperty("wind_dir")]
        public string WindDir { get; set; }

        [JsonProperty("wind_spd_kmh")]
        public int WindSpdKmh { get; set; }

        [JsonProperty("wind_spd_kt")]
        public int WindSpdKt { get; set; }
    }

    internal class Observations
    {
        [JsonProperty("notice")]
        public Notice[] Notice { get; set; }

        [JsonProperty("header")]
        public Header[] Header { get; set; }

        [JsonProperty("data")]
        public Datum[] Data { get; set; }
    }

    internal class SampleResponse1
    {
        [JsonProperty("observations")]
        public Observations Observations { get; set; }
    }
}