I saw this but it doesnot help me.
Deserializing an object with a Dictionary<string, object> property (Newtonsoft.Json)
I am working on WCF Restful service. I have complicated JSON. I have below data contracts
[DataContract]
public class TimestampPackageDC
{
[DataMember]
public string Timestamp { get; set; }
[DataMember]
public string Company { get; set; }
[DataMember]
public string ContactID { get; set; }
[DataMember]
public string Area { get; set; }
[DataMember]
public string PackageID { get; set; }
} // end of class TimestampPackageDC
[DataContract]
public class TimestampPackageExtraDC: TimestampPackageDC
{
[DataMember]
public IDictionary<string, string> ExtraInfo { get; set; }
} // end of class TimestampPackageExtraDC
[DataContract]
public class GetAllUnprintedItemsResponse: BaseResponse
{
[DataMember]
public TimestampPackageDC[] TimestampPackages { get; set; }
[DataMember]
public TimestampPackageExtraDC[] TimestampExtraInfoPackages { get; set; }
}
I am making REST service call like below
GetAllUnprintedItemsResponse upResponse = new GetAllUnprintedItemsResponse();
Request upRequest = new Request() { Name = "abc" };
string url = "http://localhost:2023/myservice/getdata";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/json";
byte[] data = Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(upRequest));
request.ContentLength = data.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(data, 0, data.Length);
requestStream.Close();
WebResponse response = request.GetResponse();
Stream respStream = response.GetResponseStream();
StreamReader reader = new StreamReader(respStream);
string responseData = reader.ReadToEnd();
//getting error below
Newtonsoft.Json.JsonConvert.PopulateObject(responseData, upResponse);
I am getting JSON string like below
{
"DocumentException": null,
"Success": true,
"TimestampExtraInfoPackages": [{
"Area ": "AA",
"Company": "XXX",
"ContactID": "123",
"PackageID": "P1",
"Timestamp": "20090501163433360001",
"ExtraInfo": [{
"Key": "Key1",
"Value": "value1"
},
{
"Key": "Key2",
"Value": "value2"
}
]
}],
"TimestampPackages": []
}
Below is the error I am getting
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'System.Collections.Generic.IDictionary`2[System.String,System.String]' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. Path 'TimestampExtraInfoPackages[0].ExtraInfo', line 1, position 194.
SOLUTION:
Thanks to Shiva, Brian and taffer. I used taffer advice and it worked. It looks we cannot use IDictionary.
I used below to solve my issue.
public List<KeyValuePair<string, string>> ExtraInfo { get; set; }
I tried below but did not work got same error.
public IDictionary<string, string>[] ExtraInfo { get; set; }