json字符串 怎么样反序列化到winform的控件

.Net技术 码拜 6年前 (2015-10-24) 877次浏览
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Newtonsoft.Json;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        string str1 = @"{""javaClass"":""DataSet"",""success"":true,""metaData"":{""root"":""rows"",""successProperty"":""success""},""rows"":[{""xm"":""wang"",""xb"":""nan"",""mingzu"":""han"",""csrj"":""20140811""},{""xm"":""zhao"",""xb"":""nan"",""mingzu"":""han"",""csrj"":""20130811""},{""xm"":""zhang"",""xb"":""nv"",""mingzu"":""han"",""csrj"":""20150811""}]}";
        public Form1()
        {
            InitializeComponent();
        }
        public class ninaji
        {
            public string javaClass { get;set;}
            public string success { get; set; }
            public metaData metaData = new metaData();
            public List<rows> bins  { get; set; }
            //public List<rows> rows= new List<rows>();
        }
        public class metaData
        {
            public string root { get;set;}
            public string successProperty { get; set; }
        };
        public class rows
        {
            public string xm { get;set;}
            public string xb { get; set; }
            public string mingzu { get; set; }
            public string csrq { get; set; }
        };
        private void Form1_Load(object sender, EventArgs e)
        {
        }
        private void button1_Click(object sender, EventArgs e)
        {
            ninaji nianji1 =new ninaji();
            nianji1 = JsonConvert.DeserializeObject<ninaji>(str1);
            textBox1.Text = nianji1.bins.ToString();
        }
    }
}

提示未将对象引用到对象实例

解决方案:40分
估计你的类有问题,下面是本人用VS自动生成的json类

public class Rootobject
{
    public string javaClass { get; set; }
    public bool success { get; set; }
    public Metadata metaData { get; set; }
    public Row[] rows { get; set; }
}
public class Metadata
{
    public string root { get; set; }
    public string successProperty { get; set; }
}
public class Row
{
    public string xm { get; set; }
    public string xb { get; set; }
    public string mingzu { get; set; }
    public string csrj { get; set; }
}

反序列化代码

string json = @"{""javaClass"":""DataSet"",""success"":true,""metaData"":{""root"":""rows"",""successProperty"":""success""},""rows"":[{""xm"":""wang"",""xb"":""nan"",""mingzu"":""han"",""csrj"":""20140811""},{""xm"":""zhao"",""xb"":""nan"",""mingzu"":""han"",""csrj"":""20130811""},{""xm"":""zhang"",""xb"":""nv"",""mingzu"":""han"",""csrj"":""20150811""}]}";
var obj = JsonConvert.DeserializeObject<Rootobject>(json);
Console.WriteLine(obj.rows[0].mingzu);

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明json字符串 怎么样反序列化到winform的控件
喜欢 (0)
[1034331897@qq.com]
分享 (0)