0

How should I parse it with Newtonsoft.Json ?

[[[671,1]],[[2011,1],[1110,1]],[[2001,1],[673,1]],[[1223,1],[617,1],[685,1]],[[671,1],[1223,2],[685,1]]]
1
  • 2
    what did you try? Commented Feb 26, 2019 at 9:11

1 Answer 1

1

You appear to have a List<List<List<int>>>> there:

using System;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace Demo
{
    class Program
    {
        static void Main()
        {
            string test = "[[[671, 1]],[[2011,1],[1110,1]],[[2001,1],[673,1]],[[1223,1],[617,1],[685,1]],[[671,1],[1223,2],[685,1]]]";

            var result = JsonConvert.DeserializeObject<List<List<List<int>>>>(test);

            for (int i = 0; i < result.Count; ++i)
            {
                Console.WriteLine($"Page: {i}");

                for (int j = 0; j < result[i].Count; ++j)
                    Console.WriteLine($"Row {j}: " + string.Join(", ", result[i][j]));
            }

        }
    }
}

This outputs:

Page: 0
Row 0: 671, 1
Page: 1
Row 0: 2011, 1
Row 1: 1110, 1
Page: 2
Row 0: 2001, 1
Row 1: 673, 1
Page: 3
Row 0: 1223, 1
Row 1: 617, 1
Row 2: 685, 1
Page: 4
Row 0: 671, 1
Row 1: 1223, 2
Row 2: 685, 1

Note that you could also use int[][][]:

var result = JsonConvert.DeserializeObject<int[][][]>(test);

If you do that, you'd have to use .Length instead of .Count of course.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much! Didn't realize Newtonsoft library support such nested generic types!

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.