解析值时遇到意外字符。路径”,第0行,位置0。'

时间:2019-03-12 16:11:35

标签: c# json api zipcode

我正在使用来自Zip-Code.com的API,因此我向它提供3个变量(邮政编码,最小距离,最大距离),并且它将返回所有邮政编码以及该半径内的城市名称。我能够发送请求,并且得到响应,但是当我尝试将json反序列化为对象时,出现以下错误: (解析值时遇到意外字符:。路径”,行0,位置0。”) 下面是我的代码以及一些我尝试过但未成功的注释掉的代码。

class Worker
{

    string zipAPIKey = "MYAPIKEY";
    string requestURL = "https://api.zip-codes.com/ZipCodesAPI.svc/1.0/";
    RootObject ZipCodesObject = new RootObject();


    public int DoWork()
    {
        GetZips();

        return 0;
    }



    public RestRequest CreateRequest(string endPoint, Method method)
    {
        var request = new RestRequest(endPoint, method);
        request.RequestFormat = DataFormat.Json;
        request.Method = method;
        request.AddHeader("Accept", "application/json");


        return request;
    }
    public void GetZips()
    {
        int minimumRadius = 0;
        int maxRadius = 20;
        string ZipCode = "40291";

        List<string> ZipList = new List<string>();

        var client = new RestClient(requestURL);
        client.BaseUrl = new Uri(requestURL);
        RestRequest request = CreateRequest(@"FindZipCodesInRadius?zipcode="+ZipCode+"&maximumradius="+maxRadius+"&minimumradius="+minimumRadius+"&key="+zipAPIKey, Method.GET);

        IRestResponse response = client.Execute(request);


        //Console.WriteLine(response.Content.GetType());
        //string removed = JsonConvert.DeserializeObject(response.Content).ToString();
        //removed.Substring(1);
        //JsonConvert.SerializeObject(response.Content);
        //response.Content.Trim(new char[] { '\uFEFF', '\u200B' });
        //string result = response.Content;
        //result = JToken.Parse(result).ToString();
        //Console.WriteLine(result);
        //result = JToken.Parse(response.Content).ToString();
        //Console.WriteLine(result);

        string json = new StreamReader(response.Content).ReadToEnd();//This fails with the same error that the DeserializeObject statement below fails with.
        Console.WriteLine(json);


        if (response.StatusCode == System.Net.HttpStatusCode.OK)
        {
            ZipCodesObject = JsonConvert.DeserializeObject<RootObject>(response.Content);// This is where I get the error


        }
    }
}

在打印到控制台时收到的响应在开始时显示一个问号。

?{
  "DataList": [
    {
      "Code": "40291",
      "City": "LOUISVILLE",
      "State": "KY",
      "Latitude": 38.131191000000,
      "Longitude": -85.574576000000,
      "County": "JEFFERSON"
    }//This is just the first result from the API.

将鼠标悬停在响应上时,内容会向我显示-----> What the response looks like before choosing a view mode

我在这里想念什么?我已经尝试了几种方法,但是无法使其正常工作。谢谢!

0 个答案:

没有答案