2

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

Screenshot for error

8
  • Please put a bit more time into formatting your code before you post - it doesn't take long, but makes a huge difference in readability. Commented Jan 13, 2015 at 16:01
  • Sure @JonSkeet. It looks great now. Commented Jan 13, 2015 at 16:02
  • 1
    Ideally, you should isolate a piece of JSON which fails, and include it in your question - preferrably after simplifying it (and your code) as far as possible. Try to come up with a short but complete program demonstrating the problem. Commented Jan 13, 2015 at 16:03
  • @JonSkeet I'm looking to download the JSON and upload on datatable. How can i find the piece of JSON which fail, any JSON validtor or something to check that? Commented Jan 13, 2015 at 16:06
  • Well you could add a try/catch around the deserialization, and just log the json if it fails... Commented Jan 13, 2015 at 16:12

1 Answer 1

4

It will work if you use these classes:

public class Collection1
{
    public string price { get; set; }
    public object 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 frequency { get; set; }
    public int version { get; set; }
    public bool newdata { get; set; }
    public string lastrunstatus { get; set; }
    public string lastsuccess { get; set; }
    public string thisversionstatus { get; set; }
    public string thisversionrun { get; set; }
    public Results results { get; set; }
}

This site will build C# classes from json auto-magically: http://json2csharp.com/

Sign up to request clarification or add additional context in comments.

2 Comments

That site is killer, saved me a bunch of time, thanks!
What if I don't want the properties to be public?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.