简体   繁体   English

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

[英]Deserializing this JSON in C# with Newtonsoft.Json

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

[[[671,1]],[[2011,1],[1110,1]],[[2001,1],[673,1]],[[1223,1],[617,1],[685,1]],[[671,1],[1223,2],[685,1]]]

You appear to have a List<List<List<int>>>> there: 您似乎在那里有一个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]));
            }

        }
    }
}

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[][][] : 注意,您也可以使用int[][][]

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

If you do that, you'd have to use .Length instead of .Count of course. 如果你这样做,你就必须使用.Length ,而不是.Count当然。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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