1
我在我的服務器上創建了一個json文件,它使用通過JSON.NET反序列化將數據發送到c#程序。然而,即時通訊即時獲得空對象的異常,任何人都可以請教我如何創建類。由於 我班在這裏json.net反序列化字符串到類
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 Newtonsoft.Json;
namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Load();
}
public void Load()
{
label1.Text = "State:\nLoading...";
try
{
Products pd = new Products();
using (var webClient = new System.Net.WebClient())
{
// download json from url
var json = webClient.DownloadString(url);
// Now parse with JSON.Net
Products convert = JsonConvert.DeserializeObject<Products>(json) as Products;
label1.Text += pd.info.ToString();
label1.Text += "\nWeb Service Connected To";
}
}
catch (JsonSerializationException jsonerr) { label1.Text += "\nWeb Service Connection Failed"; MessageBox.Show(jsonerr.ToString()); }
catch (Exception err) { throw; }
finally { label1.Text += "\nWeb Service Closed"; }
}
}
}
public class Products
{
public Info info;
[JsonProperty("post")]
public Info infos
{
get { return info; }
set { info = value; }
}
}
public class Info
{
private string pd_name;
private int pd_id;
[JsonProperty("pd_id")]
public int pd_ids
{
get { return pd_id; }
set { pd_id = value; }
}
[JsonProperty("pd_name")]
public string pd_names
{
get { return pd_name; }
set { pd_name = value; }
}
}