使用Newtonsoft反序列化Json到C#中的对象列表
因此,我正在努力解析以下JSON字符串。即使在研究了StackOverflow的许多问题之后 Json 我为此创建的类如下所示: 选项.cs使用Newtonsoft反序列化Json到C#中的对象列表,c#,json,deserialization,C#,Json,Deserialization,因此,我正在努力解析以下JSON字符串。即使在研究了StackOverflow的许多问题之后 Json 我为此创建的类如下所示: 选项.cs using System.Collections.Generic; namespace App.Models { public class Options { public ICollection<IDictionary<string, string>> text { get; set; } public
using System.Collections.Generic;
namespace App.Models
{
public class Options
{
public ICollection<IDictionary<string, string>> text { get; set; }
public List<string> cascade { get; set; }
public string val { get; set; }
}
}
使用System.Collections.Generic;
名称空间App.Models
{
公共类选项
{
公共ICollection文本{get;set;}
公共列表级联{get;set;}
公共字符串val{get;set;}
}
}
为了运行反序列化,我写了以下几行:
List<Options> optionList = JsonConvert.DeserializeObject<List<Options>>(inputString);
List optionList=JsonConvert.DeserializeObject(inputString);
当我尝试运行代码时,出现以下异常:
Newtonsoft.Json.JsonSerializationException:获取异常详细信息时超时
您的问题是读取“文本”对象。 从您的示例中,它包含字符串类型的键/值对。 没有理由在那里使用
ICollection
,只有Dictionary
公共类选项
{
公共字典文本{get;set;}
公共列表级联{get;set;}
公共字符串val{get;set;}
}
更新:
由于示例JSON不包含有关cascade
成员的数据(仅为空数组),因此可以安全地将其声明为对象列表list
Trypublic Dictionary text{get;set;}
,如果将JSON粘贴到其中,将为您提供数据结构建议。这可能不是唯一的方法,但它会提出一些可能有效的建议。注意:代码中的List
似乎不适合用于“cascade”,因为它是JSON中的一个数组。List cascade
似乎不可取,因为它只是JSON中的一个空数组-我们不知道它可能包含什么<代码>列表可能更安全
List<Options> optionList = JsonConvert.DeserializeObject<List<Options>>(inputString);
public class Options
{
public Dictionary<string, string> text { get; set; }
public List<string> cascade { get; set; }
public string val { get; set; }
}