简体   繁体   English

使用 Newtonsoft.Json 时无法将数组的 Json 字符串数组反序列化为对象

[英]Unable to deserialize Json string array of arrays into object when using Newtonsoft.Json

i am using Newtonsoft.Json and trying to deserialize an array of arrays Json string into C# object i created.我正在使用 Newtonsoft.Json 并尝试将数组 Json 字符串反序列化为我创建的 C# 对象。

This is the json string -这是 json 字符串 -

[4615,4618,4619,4626,4615,4626,4631,4636,4637],[4615,4618,4619,4626,4615,4626,4631,4636,4637],[4615,4618,4619,4626,4615,4626,4631,4636,4637]

This is my object model -这是我的对象模型 -

  public class NumberMatrix
    {
        public List<int> NumberIDs { get; set; }

        public NumberMatrix()
        {
            this.NumberIDs = new List<int>();
        }
    }

This is how i try to convert -这就是我尝试转换的方式-

var numbers = HttpContext.Current.Request.Params["Numbers"];
var numberIDsMatrix = JsonConvert.DeserializeObject<List<NumberMatrix>>(numbers);

i tried to deserialize the json in few ways, and got different errors.我试图以几种方式反序列化 json,但遇到了不同的错误。 is it possible to deserialize this json string?是否可以反序列化这个 json 字符串? how?如何?

That isn't valid JSON, you need to surround it with [...] for example.这不是有效的 JSON,例如,您需要用[...]将其括起来。 You could do this:你可以这样做:

var result = JsonConvert.DeserializeObject<List<List<int>>>($"[{numbers}]");

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

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