使用Newtonsoft.Json在C#中反序列化此JSON

时间:2019-02-26 09:08:16

标签: c#

我应该如何用Newtonsoft.Json解析它?

num1 = int(input('What is your first number? '))
num2 = int(input('What is your second number? '))
if num1 > num2:
    high = num1
    low = num2
if num1 < num2:
    low = num1
    high = num2
def main():
    sum = low
    low = low + 1
    sum = low + sum
    if low > high:
        print('The sum is', sum)
    if low < high:
        main()

1 个答案:

答案 0 :(得分:1)

您似乎在那里有一个List<List<List<int>>>>

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

        }
    }
}

这将输出:

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

请注意,您也可以使用int[][][]

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

如果这样做,则必须使用.Length而不是.Count