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

发布于 2024-10-19 20:30:52 字数 2827 浏览 3 评论 0原文

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

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; }
}

I am using Json.Net however when I go to deserialize the following json I get that Json cannot deserialize to type List.

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"}]}

code:

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; 

postalCodes class:

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; }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

安静被遗忘 2024-10-26 20:30:52

请尝试这个:

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); 
        }

Please try this:

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); 
        }
眼藏柔 2024-10-26 20:30:52

看起来您有一个数组,并且它映射到 List 而不是 postalCodes

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

Looks like you have an array and this maps to a List<postalCodes> instead of postalCodes.

var observation = JsonConvert.DeserializeObject<List<postalCodes>>(jsonResponse); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文