0

Whenever I run this program the JSON visualizer shows that all of the JSON is being read from the url, however it keeps saying that additional text is found after deserializing object.

My main goal is just to be able to read the JSON and add it to a Dataset of some sort so that I can display it

    public class Wrapper
    {
        [JsonProperty("results")]

        public DataSet DataSet { get; set; }
    }
    public class Rootobject
    {
        public int response_code { get; set; }
        public Result[] results { get; set; }
    }

    public class Result
    {
        public string category { get; set; }
        public string type { get; set; }
        public string difficulty { get; set; }
        public string question { get; set; }
        public string correct_answer { get; set; }
        public string[] incorrect_answers { get; set; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {



            string json = (new WebClient()).DownloadString("https://opentdb.com/api.php?amount=10&type=boolean");
            DataSet ds = JsonConvert.DeserializeObject<Wrapper>(json).DataSet;

        }
    }
}

}

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245

1 Answers1

0

Try this(Still updating):

I got the ToDataSet from here here

static class MyDSet
{
    public static DataSet ToMyDataSet<T>(this IList<T> list)
    {
        Type elementType = typeof(T);
        DataSet ds = new DataSet();
        DataTable t = new DataTable();
        ds.Tables.Add(t);

        //add a column to table for each public property on T
        foreach (var propInfo in elementType.GetProperties())
        {
            t.Columns.Add(propInfo.Name, propInfo.PropertyType);
        }

        //go through each property on T and add each value to the table
        foreach (T item in list)
        {
            DataRow row = t.NewRow();
            foreach (var propInfo in elementType.GetProperties())
            {
                row[propInfo.Name] = propInfo.GetValue(item, null);
            }
        }

        return ds;
    }
}

public class Wrapper
{
    [JsonProperty("results")]

    public DataSet DataSet { get; set; }
}

public class Result
{
    public string category { get; set; }
    public string type { get; set; }
    public string difficulty { get; set; }
    public string question { get; set; }
    public string correct_answer { get; set; }
    public List<string> incorrect_answers { get; set; }
}

public class RootObject
{
    public int response_code { get; set; }
    public List<Result> results { get; set; }
}

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        string json = (new WebClient()).DownloadString("https://opentdb.com/api.php?amount=10&type=boolean");
        var ds = JsonConvert.DeserializeObject<RootObject>(json);
        Wrapper wrapper = new Wrapper();
        wrapper.DataSet = ds.results.ToMyDataSet();
        Console.WriteLine(ds.response_code);
        Console.WriteLine("Hello World!");
        Console.ReadKey();
    }
}

Finally, I deserialized the Root object then converted the list of results in the root object to the data set. There might be a simplified way of writing the syntax to deserialise the object, convert and assign.

Dev
  • 1,780
  • 3
  • 18
  • 46
  • on the line that reads "wrapper.DataSet = ds.results.ToDataSet();" it underlines the ToDataSet() part and says " The call is ambiguous between the following methods or properties: 'DSet.ToDataSet(IList)' and 'DSet.ToDataSet(IList)' – jaysperceptionn Feb 22 '20 at 01:53
  • https://dotnetfiddle.net/05kFWb is it possible you have a class name `Dset` already? or a method name `ToDataSet` in a different namespace. I'll update my answer – Dev Feb 23 '20 at 16:32
  • @jaysperceptionn what environment are you working in so I can test it there? – Dev Feb 24 '20 at 17:37
  • Have you tried the Fiddle ? and I meant is this a web app eg. asp.net, or windows WPF etc? – Dev Feb 26 '20 at 10:38
  • I haven't tried the fiddle yet but I will take a look at it right now. Yes this is a web application – jaysperceptionn Feb 28 '20 at 07:24