0

I am trying to Parse JSON from a url and store it in dataset.

public override void getJobsFromSource()
    {
        string url = @"https://data.usajobs.gov/api/jobs?Country=United%20States&NumberOfJobs=1&Page=1";
        DataTable dt = new DataTable();
        DataSet data = JsonConvert.DeserializeObject<DataSet>(url);

        DataSet ds = new DataSet();
        ds.Tables.Add(dt);

    }

But I get this exception:{"Unexpected character encountered while parsing value: h. Path '', line 0, position 0."} I tried it a different way:

public override void getJobsFromSource()
    {
        string url = @"https://data.usajobs.gov/api/jobs?Country=United%20States&NumberOfJobs=1&Page=1";
        DataTable dt = (DataTable)JsonConvert.DeserializeObject(url, (typeof(DataTable)));            

        DataSet ds = new DataSet();
        ds.Tables.Add(dt);

    }

And i got the same exception.What am i doing wrong?Thanks

Iman
  • 717
  • 15
  • 32
  • 2
    `DeserializeObject` takes a string of JSON. `https://...` is not valid JSON. You're looking for `HttpClient`. – SLaks Mar 10 '16 at 20:59
  • How is a url in anyway shape or form JSON?. Are you meaning that somehow the JsonConvert.DeserializeObject will actually go and get the JSON from that URL. It will not do that. You are simply giving it a string and asking it to turn it into JSON. You need something like HTTPClient to go get the JSON first – OJay Mar 10 '16 at 21:02
  • For an example of using `HttpClient` with Json.NET see http://stackoverflow.com/questions/22675446/json-net-deserialize-directly-from-a-stream-to-a-dynamic. The extension method shown in the question should meet your needs. – dbc Mar 10 '16 at 21:10

1 Answers1

0

You're trying to parse the URL string, not the actual json content that is at the location.

Try this :

public override void getJobsFromSource()
{
    string url = @"https://data.usajobs.gov/api/jobs?Country=United%20States&NumberOfJobs=1&Page=1";

    using(WebClient client = new WebClient()) {
        string json = client.DownloadString(url);
        [...]

    }            

}
Cédric Françoys
  • 870
  • 1
  • 11
  • 22
  • Thanks, You are right now i know what i was doing wrong. but with your code i am getting "System.OutOfMemoryException' was thrown" – Iman Mar 10 '16 at 21:21
  • 1
    Now you got the data it should be easy to find out what's going on. Have a look at this topic : [Deserialize JSON String into DataTable / DataSet using c#](http://stackoverflow.com/questions/26970797/deserialize-json-string-into-datatable-dataset-using-c-sharp) – Cédric Françoys Mar 10 '16 at 21:28