簡體   English   中英

如何將帶有數組的json解析為對象

[英]How to parse json with an array into object

考慮以下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#的對象,但是,我不知道如何去數組值,因為它有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等更好的方法。 。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM