代码之家  ›  专栏  ›  技术社区  ›  Bagzli

如何将带有数组的JSON解析为对象

  •  0
  • Bagzli  · 技术社区  · 7 年前

    考虑以下JSON:

    {
      "0": {
        "id": "1",
        "email": "someemail@test.com",
        "tstamp": "2019-01-21 11:19:48",
        "times": "2",
        "tstamp_iso": "2019-01-21T12:19:48-05:00"
      },
      "1": {
        "id": "2",
        "email": "someotheremail@test.com",
        "tstamp": "2019-01-21 11:25:48",
        "times": "2",
        "tstamp_iso": "2019-01-21T12:25:48-05:00"
      },
      "result_code": 1,
      "result_message": "Success!",
      "result_output": "json"
    }
    

    不过,我正试图将该数据转换为C对象,但我不确定如何处理数组值。 0 , 1 它的名称而不是嵌套在数组中,如果有20个结果,它将一直持续到20个。 我无法更改JSON数据。

    我走了这么远:

    [JsonObject]
    public class FirstObject
    {
        [JsonProperty(PropertyName = "id")]
        public string Id { get; set; }
    
        [JsonProperty(PropertyName = "email")]
        public string Email { get; set; }
    
        [JsonProperty(PropertyName = "tstamp")]
        public string TimeStamp { get; set; }
    
        [JsonProperty(PropertyName = "times")]
        public string Times { get; set; }
    
        [JsonProperty(PropertyName = "tstamp_iso")]
        public string TimeStampIso { get; set; }
    }
    
    [JsonObject]
    public class SecondObject
    {
        public FirstObject[] FirstObjects { get; set; }
    
        [JsonProperty(PropertyName = "result_code")]
        public string ResultCode { get; set; }
    
        [JsonProperty(PropertyName = "result_message")]
        public string ResultMessage { get; set; }
    
        [JsonProperty(PropertyName = "result_output")]
        public string ResultOutput { get; set; }
    }
    

    我不明白的是如何将FirstObjects映射到0,1,…20岁。我希望有一个更好的方法,比写下20次和设置名称为0,或1,等等…

    1 回复  |  直到 7 年前
        1
  •  1
  •   Vladimir P.    7 年前
    using Newtonsoft.Json;
    using Newtonsoft.Json.Linq;
    using System;
    using System.Collections.Generic;
    
    namespace ConsoleApp2
    {
        class Program
        {
            const string json = @"{
                                      ""0"": {
                                        ""id"": ""1"",
                                        ""email"": ""someemail@test.com"",
                                        ""tstamp"": ""2019-01-21 11:19:48"",
                                        ""times"": ""2"",
                                        ""tstamp_iso"": ""2019-01-21T12:19:48-05:00""
                                      },
                                      ""1"": {
                                        ""id"": ""2"",
                                        ""email"": ""someotheremail@test.com"",
                                        ""tstamp"": ""2019-01-21 11:25:48"",
                                        ""times"": ""2"",
                                        ""tstamp_iso"": ""2019-01-21T12:25:48-05:00""
                                      },
                                      ""result_code"": 1,
                                      ""result_message"": ""Success!"",
                                      ""result_output"": ""json""
                                  }";
    
            static void Main(string[] args)
            {
                JObject o = JObject.Parse(json);
    
                List<FirstObject> l = new List<FirstObject>();
    
                int c = 0;
    
                while (o[$"{c}"] != null)
                {
                    FirstObject fo = o[$"{c++}"].ToObject<FirstObject>();
                    l.Add(fo);
                }
    
                SecondObject so = JsonConvert.DeserializeObject<SecondObject>(json);
    
                so.FirstObjects = l.ToArray();
    
                Console.ReadKey();
            }
        }
    }