I was struggling to find what's wrong with my code on JSON Deserialization and read many post but couldn't find the exact solution. The JSON is send by client, and sometime it success and sometime it fail with error "No parameterless constructor defined for type of 'System.String'." on this line : RootObject myData = new JavaScriptSerializer().Deserialize(json);
C#
DataTable dt = new DataTable();
dt.Columns.Add("isbn", typeof(string));
dt.Columns.Add("price", typeof(string));
dt.Columns.Add("uri", typeof(string));
using (var WebClient = new WebClient())
{
string json = WebClient.DownloadString("http://www.pricetree.com/internal/test.txt");
RootObject myData = new JavaScriptSerializer().Deserialize<RootObject>(json);
foreach (var item in myData.results.collection1)
{
dt.Rows.Add(item.url, item.price, item.uri);
}
}
Class
public class Collection1
{
public string price { get; set; }
public string uri { get; set; }
public string url { get; set; }
}
public class Results
{
public List<Collection1> collection1 { get; set; }
}
public class RootObject
{
public string name { get; set; }
public int count { get; set; }
public string lastrunstatus { get; set; }
public string lastsuccess { get; set; }
public Results results { get; set; }
}
I already seen some answer below from below thread, but that didn't work. Can anyone suggest me the right solution.
No parameterless constructor defined for type of 'System.String' during JSON deserialization
