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.