【问题标题】:How to parse json with an array into object如何将带有数组的json解析为对象
【发布时间】:2019-01-21 17:40:14
【问题描述】:

考虑以下 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"
}

我正在尝试将该数据转换为 ac# 对象,但是,我不确定如何处理数组值,因为它的名称为 01,而不是嵌套在数组中如果有 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 更好的方法,等等……

【问题讨论】:

    标签: c# arrays json


    【解决方案1】:
    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();
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-21
      • 2023-03-22
      • 2018-04-26
      相关资源
      最近更新 更多