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

3
  • 2
    DeserializeObject takes a string of JSON. https://... is not valid JSON. You're looking for HttpClient. Commented Mar 10, 2016 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 Commented Mar 10, 2016 at 21:02
  • For an example of using HttpClient with Json.NET see stackoverflow.com/questions/22675446/…. The extension method shown in the question should meet your needs. Commented Mar 10, 2016 at 21:10

1 Answer 1

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);
        [...]

    }            

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

2 Comments

Thanks, You are right now i know what i was doing wrong. but with your code i am getting "System.OutOfMemoryException' was thrown"
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#

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.