gpt4 book ai didi

c# - 反序列化 json 字符串以获得多个结果

转载 作者:行者123 更新时间:2023-11-30 21:16:31 25 4
gpt4 key购买 nike

我正在使用 Json.Net,但是当我反序列化以下 json 时,我发现 Json 无法反序列化为类型列表。

JSON:

{"postalCodes":[{"adminName2":"New York","adminCode2":"061","adminCode1":"NY","postalCode":"10001","countryCode":"US","lng":-73.996705,"placeName":"New York City","lat":40.74838,"adminName1":"New York"},{"adminName2":"New York","adminCode2":"061","adminCode1":"NY","postalCode":"10019","countryCode":"US","lng":-73.985834,"placeName":"New York City","lat":40.765069,"adminName1":"New York"},{"adminName2":"New York","adminCode2":"061","adminCode1":"NY","postalCode":"10021","countryCode":"US","lng":-73.958805,"placeName":"New York City","lat":40.768476,"adminName1":"New York"},{"adminName2":"New York","adminCode2":"061","adminCode1":"NY","postalCode":"10022","countryCode":"US","lng":-73.965703,"placeName":"New York City","lat":40.757091,"adminName1":"New York"},{"adminName2":"New York","adminCode2":"061","adminCode1":"NY","postalCode":"10036","countryCode":"US","lng":-73.991826,"placeName":"New York City","lat":40.759724,"adminName1":"New York"},{"adminName2":"New York","adminCode2":"061","adminCode1":"NY","postalCode":"10065","countryCode":"US","lng":-73.96379,"placeName":"New York City","lat":40.76507,"adminName1":"New York"},{"adminName2":"New York","adminCode2":"061","adminCode1":"NY","postalCode":"10003","countryCode":"US","lng":-73.989223,"placeName":"New York City","lat":40.731253,"adminName1":"New York"},{"adminName2":"New York","adminCode2":"061","adminCode1":"NY","postalCode":"10010","countryCode":"US","lng":-73.981328,"placeName":"New York City","lat":40.737476,"adminName1":"New York"},{"adminName2":"New York","adminCode2":"061","adminCode1":"NY","postalCode":"10011","countryCode":"US","lng":-73.99963,"placeName":"New York City","lat":40.740225,"adminName1":"New York"},{"adminName2":"New York","adminCode2":"061","adminCode1":"NY","postalCode":"10012","countryCode":"US","lng":-73.998284,"placeName":"New York City","lat":40.72553,"adminName1":"New York"}]}

代码:

string jsonResponse = string.empty;

using (StreamReader sr = new StreamReader(client.OpenRead(address.AbsoluteUri)))
{
jsonResponse = sr.ReadLine();
}


var observation = JsonConvert.DeserializeObject<List<postalCodes>>(jsonResponse);
return observation;

邮政编码类:

public class postalCodes
{

public string adminName2 { get; set; }

public int adminCode2 { get; set; }

public string adminCode1 {get; set; }

public int postalCode { get; set; }

public string countryCode { get; set; }

public double longitude { get; set; }

public string placeName { get; set; }

public double latitude { get; set; }

public string adminName1 { get; set; }
}

最佳答案

请试试这个:

public class postalCodes
{

public string adminName2 { get; set; }

public int adminCode2 { get; set; }

public string adminCode1 { get; set; }

public int postalCode { get; set; }

public string countryCode { get; set; }

public double lng { get; set; }

public string placeName { get; set; }

public double lat { get; set; }

public string adminName1 { get; set; }
}
public class DataContainer
{
public List<postalCodes> postalCodes { get; set; }
}
[Test]
public void TestJson()
{
string input = "{\"postalCodes\":[{\"adminName2\":\"New York\",\"adminCode2\":\"061\",\"adminCode1\":\"NY\",\"postalCode\":\"10001\",\"countryCode\":\"US\",\"lng\":-73.996705,\"placeName\":\"New York City\",\"lat\":40.74838,\"adminName1\":\"New York\"},{\"adminName2\":\"New York\",\"adminCode2\":\"061\",\"adminCode1\":\"NY\",\"postalCode\":\"10019\",\"countryCode\":\"US\",\"lng\":-73.985834,\"placeName\":\"New York City\",\"lat\":40.765069,\"adminName1\":\"New York\"},{\"adminName2\":\"New York\",\"adminCode2\":\"061\",\"adminCode1\":\"NY\",\"postalCode\":\"10021\",\"countryCode\":\"US\",\"lng\":-73.958805,\"placeName\":\"New York City\",\"lat\":40.768476,\"adminName1\":\"New York\"},{\"adminName2\":\"New York\",\"adminCode2\":\"061\",\"adminCode1\":\"NY\",\"postalCode\":\"10022\",\"countryCode\":\"US\",\"lng\":-73.965703,\"placeName\":\"New York City\",\"lat\":40.757091,\"adminName1\":\"New York\"},{\"adminName2\":\"New York\",\"adminCode2\":\"061\",\"adminCode1\":\"NY\",\"postalCode\":\"10036\",\"countryCode\":\"US\",\"lng\":-73.991826,\"placeName\":\"New York City\",\"lat\":40.759724,\"adminName1\":\"New York\"},{\"adminName2\":\"New York\",\"adminCode2\":\"061\",\"adminCode1\":\"NY\",\"postalCode\":\"10065\",\"countryCode\":\"US\",\"lng\":-73.96379,\"placeName\":\"New York City\",\"lat\":40.76507,\"adminName1\":\"New York\"},{\"adminName2\":\"New York\",\"adminCode2\":\"061\",\"adminCode1\":\"NY\",\"postalCode\":\"10003\",\"countryCode\":\"US\",\"lng\":-73.989223,\"placeName\":\"New York City\",\"lat\":40.731253,\"adminName1\":\"New York\"},{\"adminName2\":\"New York\",\"adminCode2\":\"061\",\"adminCode1\":\"NY\",\"postalCode\":\"10010\",\"countryCode\":\"US\",\"lng\":-73.981328,\"placeName\":\"New York City\",\"lat\":40.737476,\"adminName1\":\"New York\"},{\"adminName2\":\"New York\",\"adminCode2\":\"061\",\"adminCode1\":\"NY\",\"postalCode\":\"10011\",\"countryCode\":\"US\",\"lng\":-73.99963,\"placeName\":\"New York City\",\"lat\":40.740225,\"adminName1\":\"New York\"},{\"adminName2\":\"New York\",\"adminCode2\":\"061\",\"adminCode1\":\"NY\",\"postalCode\":\"10012\",\"countryCode\":\"US\",\"lng\":-73.998284,\"placeName\":\"New York City\",\"lat\":40.72553,\"adminName1\":\"New York\"}]}";
var observation = JsonConvert.DeserializeObject<DataContainer>(input);
}

关于c# - 反序列化 json 字符串以获得多个结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5163044/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com