简体   繁体   English

使用json.net反序列化json对象数组

[英]Deserialize json object array with json.net

I want to deserialize json object array. 我想反序列化json对象数组。 I'm stuck. 我被卡住了。 I can not figure out how to get it to be happy with the provided structure. 我不知道如何使它对所提供的结构感到满意。 Doing something a CustomerList (below) results in a "Cannot deserialize the current JSON array" exception. 对CustomerList(如下)执行操作会导致“无法反序列化当前JSON数组”异常。

I tried almost anything 我几乎尝试了一切

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;

namespace ConsoleAppProva
{
    class Program
    {
        public class CustomerJson
        {
            [JsonProperty("IdPostazionee")]
            public Customer Customer { get; set; }
        }

        public class Customer
        {
            [JsonProperty("abc")]
            public string Firstname { get; set; }

            [JsonProperty("def")]
            public string Lastname { get; set; }

        }

        static void Main(string[] args)
        {
            string json = "{'IdPostazione':'1','StatoAutoma':'2','OriginalURL':'3','OriginalTitle':'lol','ChronicID':'xd'}";

            dynamic dynObj = JsonConvert.DeserializeObject(json);

            Console.WriteLine("{0} {1} {2}", dynObj.IdPostazione, dynObj.StatoAutoma, dynObj.OriginalURL);

            string jsoon = "{'IdPostazionee':['abc':'123','def':'456']}";

            Console.ReadLine();
        }
    }
}

I'm expecting to see in console the values of the array : 123 , 456. 我期望在控制台中看到数组的值:123,456。

IdPostazionee is the array. IdPostazionee是数组。 abc, def are the fields abc,def是字段

The following JSON that you posted is invalid: 您发布的以下JSON无效:

{
    'IdPostazionee': ['abc': '123', 'def': '456']
}

I guess it should be: 我想应该是:

{
    "IdPostazionee": [{
        "abc": "123",
        "def": "456"
    }]
}

Or better: 或更好:

{
    "IdPostazionee": {
        "abc": "123",
        "def": "456"
    }
}

The following code should work: 下面的代码应该工作:

using Newtonsoft.Json;
using System;

namespace ConsoleApp11
{
    class Program
    {
        public class CustomerJson
        {
            [JsonProperty("IdPostazionee")]
            public Customer Customer { get; set; }
        }

        public class Customer
        {
            [JsonProperty("abc")]
            public string Firstname { get; set; }

            [JsonProperty("def")]
            public string Lastname { get; set; }

        }

        static void Main(string[] args)
        {
            string json = "{'IdPostazione':'1','StatoAutoma':'2','OriginalURL':'3','OriginalTitle':'lol','ChronicID':'xd'}";

            dynamic dynObj = JsonConvert.DeserializeObject(json);

            Console.WriteLine("{0} {1} {2}", dynObj.IdPostazione, dynObj.StatoAutoma, dynObj.OriginalURL);

            string jsoon = "{'IdPostazionee': {'abc':'123','def':'456'}}";

            var customerJson = JsonConvert.DeserializeObject<CustomerJson>(jsoon);

            Console.WriteLine(customerJson.Customer.Firstname);
            Console.WriteLine(customerJson.Customer.Lastname);
        }
    }
}

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

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