0

Consider the following json:

{
  "0": {
    "id": "1",
    "email": "[email protected]",
    "tstamp": "2019-01-21 11:19:48",
    "times": "2",
    "tstamp_iso": "2019-01-21T12:19:48-05:00"
  },
  "1": {
    "id": "2",
    "email": "[email protected]",
    "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 a c# 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...

1 Answer 1

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"": ""[email protected]"",
                                    ""tstamp"": ""2019-01-21 11:19:48"",
                                    ""times"": ""2"",
                                    ""tstamp_iso"": ""2019-01-21T12:19:48-05:00""
                                  },
                                  ""1"": {
                                    ""id"": ""2"",
                                    ""email"": ""[email protected]"",
                                    ""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();
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.