deserializing Json data which contain an array in c#

我与影子孤独终老i 提交于 2019-12-11 08:46:01

问题


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.

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"
    }]
}]

My class:

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

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

Thank you for your help.


回答1:


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

Goto: Edit -> Paste special -> Paste JSON as Class

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; }
        }

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

Proof of concept: .net Fiddler




回答2:


You didn't declare your list as a member of a data contract in KMessage. That means it isn't serializable.

Try to add it to your class KMessage.

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


来源:https://stackoverflow.com/questions/51817656/deserializing-json-data-which-contain-an-array-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!