-2

I have JSON data that I need to access from C#. The chapters element looks like this:

"chapters": [
    [
      2, 
      1416420134.0, 
      "2", 
      "546cdb2645b9efbff4582d51"
    ], 
    [
      1, 
      1411055241.0, 
      null, 
      "541afe8945b9ef69885d3d74"
    ], 
    [
      0, 
      1414210972.0, 
      "0", 
      "544b259c45b9efb061521235"
    ]
]

Here are my C# classes that are meant to contain that data:

public class test
{
    public string[] chapters { get; set; }
}

public class TChapter
{
    public test[] aa { get; set; }
}

How can I parse the JSON to C# objects?

jlavallet
  • 1,267
  • 1
  • 12
  • 33
uğur
  • 7
  • 2

1 Answers1

0

Using Newtonsoft JSON you will want to do something like the following

using System;
using Newtonsoft.Json;

namespace JsonDeserializationTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var chaptersAsJson = "[" +
                                 "    [" +
                                 "        2," +
                                 "        1416420134.0," +
                                 "        \"2\"," +
                                 "        \"546cdb2645b9efbff4582d51\"" +
                                 "    ], " +
                                 "    [" +
                                 "        1," +
                                 "        1411055241.0," +
                                 "        null," +
                                 "        \"541afe8945b9ef69885d3d74\"" +
                                 "    ], " +
                                 "    [" +
                                 "        0," +
                                 "        1414210972.0," +
                                 "        \"0\"," +
                                 "        \"544b259c45b9efb061521235\"" +
                                 "    ]" +
                                 "]";
            var chaptersAsTwoDObjectArray = JsonConvert.DeserializeObject<object[][]>(chaptersAsJson);

            // Use the chapters array
            foreach (object[] chapter in chaptersAsTwoDObjectArray)
            {
                // what do you want to do with the object array?
                Console.WriteLine(String.Join(", ", chapter));
            }

            Console.WriteLine("Finished.");
        }
    }
}

Note that your classes don't line up with your JSON.

jlavallet
  • 1,267
  • 1
  • 12
  • 33
  • So your chapters element is an array of object arrays? And those object arrays contain an integer, a decimal, a string, and a string? Did you design this JSON? Can it be changed? What is the purpose of the four objects in the object array? – jlavallet Jun 23 '17 at 18:51