Reputation: 1103
I want to deserialize the json string into c# object which is of the following format
[{
"data": "{\"SecureId\":null,\"FNAME\":\"WILL\",\"MNAME\":null,\"LNAME\":\"CLINTON\",\"POSTNAME\":\"Mrs.\",\"DOB\":\"6/10/1997\",\"GENDER\":\"M\",\"ORGID\":null}",
"isAvial": true,
"details": "55",
"id": "1999089"}, {
"data": "{\"SecureId\":null,\"FNAME\":\"RIN\",\"MNAME\":null,\"LNAME\":\"MANN\",\"POSTNAME\":\"Jr\",\"DOB\":\"9/16/1997\",\"GENDER\":\"F\",\"ORGID\":null}",
"isAvial": true,
"details": "P2145",
"id": "1999090"}, {
"data": "{\"SecureId\":null,\"FNAME\":\"RIN\",\"MNAME\":null,\"LNAME\":\"MANNING\",\"POSTNAME\":\"M.D.\",\"DOB\":\"9/16/1997\",\"GENDER\":\"F\",\"ORGID\":null}",
"isAvial": true,
"details": "205",
"id": "1999091"}, {
"data": "{\"SecureId\":null,\"FNAME\":\"David\",\"MNAME\":null,\"LNAME\":\"O'PETER\",\"POSTNAME\":\"Dr\",\"DOB\":\"9/6/1997\",\"GENDER\":\"M\",\"ORGID\":null}",
"isAvial": true,
"details": "PM25",
"id": "1999092"}]
I have tried the following but it didn't worked. I'm using Newtonsoft.json
public class DataObj
{
public string isAvial { get; set; }
public string details { get; set; }
public string id { get; set; }
public DataTable data { get; set; }
}
public class JsonOutput
{
public List<DataObj> DetailsObj { get; set; }
}
var desc = JsonConvert.DeserializeObject<List<JsonOutput>>(jsonstring);
thanks for help..
Upvotes: 0
Views: 301
Reputation: 2741
Your deserialzing class should be like
public class RootObject
{
private object _data;
public object data
{
get
{
return _data;
}
set
{
_data = JsonConvert.DeserializeObject<dataS>(value.ToString());
}
}
public bool isAvial { get; set; }
public string details { get; set; }
public string id { get; set; }
}
public class dataS
{
public object SecureId { get; set; }
public string FNAME { get; set; }
public object MNAME { get; set; }
public string LNAME { get; set; }
public string POSTNAME { get; set; }
public string DOB { get; set; }
public string GENDER { get; set; }
public object ORGID { get; set; }
}
And serialization should be like
var tmp = JsonConvert.DeserializeObject<List<RootObject>>(json);
Upvotes: 0
Reputation: 261
If the format is correct in "data". Then this will solve
public class DataObj
{
public string isAvial { get; set; }
public string details { get; set; }
public string id { get; set; }
public DataTable data { get; set; }
}
var desc = JsonConvert.DeserializeObject<List<DataObj>>(jsonstring);
else separate parsing Datatable or something like this (DataTable)JsonConvert.DeserializeObject(JsonString, (typeof(DataTable)));
Upvotes: 1
Reputation: 1675
try
public class dataC
{
public int SecureId { get; set; }
public string FNAME { get; set; }
public string MNAME { get; set; }
public string LNAME { get; set; }
public string POSTNAME { get; set; }
public string DOB { get; set; }
public string GENDER { get; set; }
public int ORGID { get; set; }
}
public class DataObj
{
public dataC data { get; set; }
public bool isAvial { get; set; }
public string details { get; set; }
public string id { get; set; }
}
JsonConvert.DeserializeObject<List<DataObj>>(jsonstring);
Upvotes: 0
Reputation: 727
In your JSON object, you have a list of DataObj objects, so (provided the DataTable class is described correctly according to the JSON structure) you should use the following to deserialize:
var desc = JsonConvert.DeserializeObject<List<DataObj>>(jsonstring);
Upvotes: 0
Reputation: 62213
Its failing because your JSON is an array but you are trying to deserialize to an object that contains an array instead of directly to an array or list. Also I have serious doubts that your property data
can be deserialized as is, you will probably have to write custom code to deserialize a DataTable
or you should change it to a generic list/collection using a custom type.
public class DataObj
{
public string isAvial { get; set; }
public string details { get; set; }
public string id { get; set; }
// public DataTable data { get; set; } // if this is the FCL defined DataTable you will not be able to deserialize it without some custom code. Better would be to have a strongly typed custom class
}
var desc = JsonConvert.DeserializeObject<List<DataObj>>(jsonstring);
Upvotes: 1