简体   繁体   中英

How to parse json with an array into object

Consider the following 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"
}

I am trying to convert that data into ac# object, however, I'm not sure how to go about the array value as it has 0 , 1 for its name instead of it being nested in an array and it will go on up until 20 if there are 20 results. I can't change the json data.

I got this far:

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

What I don't understand is how to map FirstObjects to results of 0, 1, ... 20. I am hoping there is a better way than writing out that 20 times and setting the name to 0, or 1, etc...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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