反序列化包含c#中的数组的Json数据

时间:2018-08-13 07:58:03

标签: c# json list serializer

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

例如我的JSON消息:

[{
    "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”的列表为空:

  [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());
            }
        }
    }

谢谢您的帮助。

2 个答案:

答案 0 :(得分:2)

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

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

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

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

        //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库。

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 Fiddler

答案 1 :(得分:0)

您没有在KMessage中声明您的列表是数据合同的成员。 这意味着它不可序列化。

尝试将其添加到您的课程KMessage

[DataMember]
public List<TParkList> ParkList
    {
        get
        {
            return this.ParkList;
        }
        set
        {
            this.ParkList = value;
        }
    }