反序列化包含c#中的数组的Json数据 [英] deserializing Json data which contain an array in c#

查看:92
本文介绍了反序列化包含c#中的数组的Json数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在反序列化Json消息中的另一个列表时遇到问题. 我的代码可以很好地读取诸如Project,Ref,Latitude之类的数据... 我也尝试了两种方法:列表和数组.

I have a problem for deserializing another list inside Json message. My code work fine for read data like Project, Ref, Latitude ... I tried both method too : list and array.

例如我的JSON消息:

For example my JSON message:

[{
    "Project":"example", 
    "Ref":"001BC50C70000A21", 
    "Latitude":43.643166, 
    "Longitude":1.454769, 
    "ParkList": [{
        "Id":"001BC50C70000A21P1", 
        "State":1, 
        "DateTime":"2018-02-15T08:07:18.987Z"
    }, {
        "Id":"001BC50C70000A21P2",
        "State":1,
        "DateTime":"2018-02-15T08:11:41.824Z"
    }]
}]

我的课:

[DataContract]
public class KMessage
{
    private string ProjectField;
    private string RefField;
    private float LatitudeField;
    private float LongitudeField;
    public List<TParkList> ParkList { get; set; }

    [DataMember]
    public string Project
    {
        get
        {
            return this.ProjectField;
        }
        set
        {
            this.ProjectField = value;
        }
    }
    [DataMember]
    public string Ref
    {
        get
        {
            return this.RefField;
        }
        set
        {
            this.RefField = value;
        }
    }
    [DataMember]
    public float Latitude
    {
        get
        {
            return this.LatitudeField;
        }
        set
        {
            this.LatitudeField = value;
        }
    }
    [DataMember]
    public float Longitude
    {
        get
        {
            return this.LongitudeField;
        }
        set
        {
            this.LongitudeField = value;
        }
    }
}

[DataContract]
public class TParkList
{
    private string IdField;
    private int StateField;
    private string DateTimeField;

    [DataMember]
    public string Id
    {
        get
        {
            return this.IdField;
        }
        set
        {
            this.IdField = value;
        }
    }
    [DataMember]
    public int State
    {
        get
        {
            return this.StateField;
        }
        set
        {
            this.StateField = value;
        }
    }
    [DataMember]
    public string DateTime
    {
        get
        {
            return this.DateTimeField;
        }
        set
        {
            this.DateTimeField = value;
        }
    }
}

以及测试的主要代码.因此,"kmsg.ParkList"的列表为空:

And the main code for test. So the list is empty for "kmsg.ParkList":

  [ServiceContract]
    public interface IKService
    {
        [DataContractFormat] 
        [OperationContract] 
        [WebInvoke(
             Method = "POST",
             BodyStyle = WebMessageBodyStyle.Bare,
             RequestFormat = WebMessageFormat.Json,
             ResponseFormat = WebMessageFormat.Json,
             UriTemplate = "/state")]
        void Service(Stream KStream); 
    }

    public class KService : IKService
    {
        public void Service(Stream KStream)
        {
            try
            {
                if (KStream == null)
                {
                    MessageBox.Show("WebService message is NULL");
                }

                else
                {
                    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(List<KMessage>));
                    var obj = (List<KMessage>)serializer.ReadObject(KStream);

                    foreach (KMessage kmsg in obj)
                    {
                        MessageBox.Show("Project", kmsg.Project);
                        foreach (var pklist in kmsg.ParkList)
                        {
                            MessageBox.Show("List - Id", pklist.Id.ToString());
                        }
                    }
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }

谢谢您的帮助.

推荐答案

您可以使用Visual Studio提供的将json转换为模型类的功能

You can use function provided by Visual studio which convert your json into a model class

转到:编辑->粘贴特殊->将JSON粘贴为类

此功能创建的模型类将解决您的问题

Model class created by this feature will solve your problem

我使用了此功能并为您创建了模型类.

I used this feature and created model class for you.

        //Use respective attributes 
        public class KMessage
        {
            public string Project { get; set; }
            public string Ref { get; set; }
            public float Latitude { get; set; }
            public float Longitude { get; set; }
            //I guess here is the problem, instead of array you can use list
            public Parklist[] ParkList { get; set; } 
        }

        public class Parklist
        {
            public string Id { get; set; }
            public int State { get; set; }
            public DateTime DateTime { get; set; }
        }

我想您在反序列化时丢失了一些东西,这是您可以反序列化json字符串的方法.我正在使用 Newtonsoft.Json 库.

I guess you are missing something, while deserialization, here is how you can deserialize your json string. I am using Newtonsoft.Json library.

string jsonString = "[{\"Project\":\"example\", \"Ref\":\"001BC50C70000A21\", \"Latitude\":43.643166, \"Longitude\":1.454769, \"ParkList\": [{\"Id\":\"001BC50C70000A21P1\", \"State\":1, \"DateTime\":\"2018-02-15T08:07:18.987Z\"}, {\"Id\":\"001BC50C70000A21P2\",\"State\":1,\"DateTime\":\"2018-02-15T08:11:41.824Z\"}]}]";

//You used [square brackets] at first, so we need to deserialize with list
List<KMessage> kMsg = JsonConvert.DeserializeObject<List<KMessage>>(jsonString);
foreach (Parklist item in kMsg[0].ParkList)
     {
         Console.WriteLine("Id: " + item.Id + " State: " + item.State + " DateTime: " + item.DateTime);
     }


Output:

Id: 001BC50C70000A21P1 State: 1 DateTime: 2/15/2018 8:07:18 AM
Id: 001BC50C70000A21P2 State: 1 DateTime: 2/15/2018 8:11:41 AM


概念证明: .net提琴手

这篇关于反序列化包含c#中的数组的Json数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆