C# 解析JSON数据时遇到问题

C# 解析JSON数据时遇到问题,c#,json,C#,Json,我有来自Web服务的以下JSON数据: {"zip_codes":[{"zip_code":"36451","distance":0,"city":"Grove Hill","state":"AL"}, {"zip_code":"36446","distance":8.031,"city":"Fulton","state":"AL"}]} 我正在使用.Net的Newtonsoft.Json库,并试图为城市解析上述字符串。我总是出错 public List<string>

我有来自Web服务的以下JSON数据:

 {"zip_codes":[{"zip_code":"36451","distance":0,"city":"Grove    Hill","state":"AL"},   {"zip_code":"36446","distance":8.031,"city":"Fulton","state":"AL"}]}
我正在使用.Net的Newtonsoft.Json库,并试图为城市解析上述字符串。我总是出错

public List<string> getZipCodesWithinRadius(string zipCode, string radius)
        {
            List<string> l = new List<string>();

            string apiKey = @"----";
            string apiFormat = @"radius.json";
            string baseUrl = @"http://www.zipcodeapi.com/rest/";

            string url = baseUrl + apiKey + @"/" + apiFormat + @"/" + zipCode + @"/" + radius + @"/mile";


            WebClient client = new WebClient();

            Stream stream = client.OpenRead(@"http://www.zipcodeapi.com/rest/uOrUEaRSt9KTqO0OuIFXxwH0uP7uX6AsU2mzcesGncAN1BCvjhtQNbRi0YNX4oqZ/radius.json/36451/10/mile");

            StreamReader rdr = new StreamReader(stream);

            List<ZipCode> ll = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ZipCode>>(rdr.Read().ToString());


            foreach (ZipCode z in ll)
            {
                l.Add(z.city);
            }
            return l;
        }
这是我的目标:

 public class ZipCode
    {
        public ZipCode() { }
        [JsonProperty(PropertyName="zip_code")]
        public string zip_code { get; set; }
         [JsonProperty(PropertyName = "distance")]
        public double distance { get; set; }
         [JsonProperty(PropertyName = "city")]
        public string city { get; set; }
         [JsonProperty(PropertyName = "state")]
        public string state { get; set; }
    }
请先帮助

,转到并在那里发布您的JSON,然后返回以下c#类:

顺便说一下,您实际上并没有将输入参数传递给您的服务——所使用的URL是硬编码的。因此,这仅仅是测试代码吗?看起来是这样,因为如果我使用
client.OpenRead(url)
,那么一切都会按预期工作

另外,您的方法不应该被调用
getCitiesWithinRadius()
?它返回的是城市,而不是邮政编码。此外,对于具有多个邮政编码的城市,它会返回重复的城市名称。

首先,转到并在那里发布您的JSON,然后返回以下c#类:

顺便说一下,您实际上并没有将输入参数传递给您的服务——所使用的URL是硬编码的。因此,这仅仅是测试代码吗?看起来是这样,因为如果我使用
client.OpenRead(url)
,那么一切都会按预期工作

另外,您的方法不应该被调用
getCitiesWithinRadius()
?它返回的是城市,而不是邮政编码。此外,对于具有多个邮政编码的城市,它会返回重复的城市名称。

首先,转到并在那里发布您的JSON,然后返回以下c#类:

顺便说一下,您实际上并没有将输入参数传递给您的服务——所使用的URL是硬编码的。因此,这仅仅是测试代码吗?看起来是这样,因为如果我使用
client.OpenRead(url)
,那么一切都会按预期工作

另外,您的方法不应该被调用
getCitiesWithinRadius()
?它返回的是城市,而不是邮政编码。此外,对于具有多个邮政编码的城市,它会返回重复的城市名称。

首先,转到并在那里发布您的JSON,然后返回以下c#类:

顺便说一下,您实际上并没有将输入参数传递给您的服务——所使用的URL是硬编码的。因此,这仅仅是测试代码吗?看起来是这样,因为如果我使用
client.OpenRead(url)
,那么一切都会按预期工作


另外,您的方法不应该被调用
getCitiesWithinRadius()
?它返回的是城市,而不是邮政编码。此外,对于具有多个邮政编码的城市,它会返回重复的城市名称。

由于根对象不是列表/数组,因此会出现此错误。(您的根对象有一个属性zip_codes,它返回一个邮政编码列表)

您可以通过以下方式获得您的列表:

using(WebClient client = new WebClient())
{
    var json = client.DownloadString(url);
    List<ZipCode> ll = JObject.Parse(json)["zip_codes"].ToObject<List<ZipCode>>();
}
使用(WebClient=newWebClient())
{
var json=client.DownloadString(url);
List ll=JObject.Parse(json)[“zip_代码”].ToObject();
}

由于根对象不是列表/数组,因此出现此错误。(您的根对象有一个属性zip_codes,它返回一个邮政编码列表)

您可以通过以下方式获得您的列表:

using(WebClient client = new WebClient())
{
    var json = client.DownloadString(url);
    List<ZipCode> ll = JObject.Parse(json)["zip_codes"].ToObject<List<ZipCode>>();
}
使用(WebClient=newWebClient())
{
var json=client.DownloadString(url);
List ll=JObject.Parse(json)[“zip_代码”].ToObject();
}

由于根对象不是列表/数组,因此出现此错误。(您的根对象有一个属性zip_codes,它返回一个邮政编码列表)

您可以通过以下方式获得您的列表:

using(WebClient client = new WebClient())
{
    var json = client.DownloadString(url);
    List<ZipCode> ll = JObject.Parse(json)["zip_codes"].ToObject<List<ZipCode>>();
}
使用(WebClient=newWebClient())
{
var json=client.DownloadString(url);
List ll=JObject.Parse(json)[“zip_代码”].ToObject();
}

由于根对象不是列表/数组,因此出现此错误。(您的根对象有一个属性zip_codes,它返回一个邮政编码列表)

您可以通过以下方式获得您的列表:

using(WebClient client = new WebClient())
{
    var json = client.DownloadString(url);
    List<ZipCode> ll = JObject.Parse(json)["zip_codes"].ToObject<List<ZipCode>>();
}
使用(WebClient=newWebClient())
{
var json=client.DownloadString(url);
List ll=JObject.Parse(json)[“zip_代码”].ToObject();
}
rdr.Read()
只读取一个字符。您可能想使用
ReadToEnd()
。但它仍然无法工作,您可能需要一个
ZipCodes
类,该类的属性是
ZipCode
的列表,并反序列化为该类型。不要公开发布api密钥。
rdr.Read()
只读取一个字符。您可能想使用
ReadToEnd()
。但它仍然无法工作,您可能需要一个
ZipCodes
类,该类的属性是
ZipCode
的列表,并反序列化为该类型。不要公开发布api密钥。
rdr.Read()
只读取一个字符。您可能想使用
ReadToEnd()
。但它仍然无法工作,您可能需要一个
ZipCodes
类,该类的属性是
ZipCode
的列表,并反序列化为该类型。不要公开发布api密钥。
rdr.Read()
只读取一个字符。您可能想使用
ReadToEnd()
。但它仍然无法工作,您可能需要一个
ZipCodes
类,该类的属性是
ZipCode
的列表,并反序列化为该类型。不要公开发布api密钥。