简体   繁体   English

从网址解析json并将其存储在数据集中。 解析时遇到意外字符

[英]Parsing json from url and storing it in dataset. Unexpected character encountered while parsing

I am trying to Parse JSON from a url and store it in dataset. 我正在尝试从URL解析JSON并将其存储在数据集中。

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: 但是我遇到了这个异常:{“解析值时遇到意外字符:h。Path”,第0行,位置0。”}我尝试了另一种方法:

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 而且我也有同样的例外。我在做什么错?

You're trying to parse the URL string, not the actual json content that is at the location. 您正在尝试解析URL字符串,而不是该位置的实际json内容。

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

    }            

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM