C# 使用Newtonsoft中的PopulateObject函数时发生JsonSerializationException
用C#编码,我被一个愚蠢的JSON反序列化阻止了,希望你的专家眼睛能找到我缺少的东西 我正在尝试提取以下JSON文件:C# 使用Newtonsoft中的PopulateObject函数时发生JsonSerializationException,c#,json,exception,json.net,C#,Json,Exception,Json.net,用C#编码,我被一个愚蠢的JSON反序列化阻止了,希望你的专家眼睛能找到我缺少的东西 我正在尝试提取以下JSON文件: { “文件”: { “版本”:“1.0.0”, “最新更新”:“10.08.2020”, “说明”:“本文件列举了制造商的能力”, “Id”:” }, “能力”:[ { “Id”:0, “说明”:“, “启用”:正确, “RelativePath”:“Company1\\manifest.json” }, { “Id”:1, “说明”:“, “启用”:正确, “Relative
{
“文件”:
{
“版本”:“1.0.0”,
“最新更新”:“10.08.2020”,
“说明”:“本文件列举了制造商的能力”,
“Id”:”
},
“能力”:[
{
“Id”:0,
“说明”:“,
“启用”:正确,
“RelativePath”:“Company1\\manifest.json”
},
{
“Id”:1,
“说明”:“,
“启用”:正确,
“RelativePath”:“Company2\\manifest.json”
}
]
}
ManifestDescriptor类应通过其构造函数调用自提取(并存储)JSON数据。实施情况如下:
使用JsonTools;
使用Newtonsoft.Json;
使用Newtonsoft.Json.Linq;
使用Serilog;
使用会话服务;
使用制度;
使用System.Collections.Generic;
使用System.IO;
使用系统文本;
命名空间BaseService.Manifest
{
公共类描述符:JObject
{
///
///包含Json文档描述符数据
///
私人文件组文件;
私有列表容量;
///
///包含Json文档描述符数据
///
[JsonProperty(“文件”)]
公共文件组文件
{
获取=>\u文档;
设置
{
如果(值!=\u文档)
{
_文件=价值;
}
}
}
///
///包含文档容量
///
[JsonProperty(“容量”)]
公开名单能力
{
获取=>\u容量;
内部集
{
如果(值)=\u容量)
{
_容量=价值;
}
}
}
公共ManifestDescriptor(字符串绝对路径)
{
string l_rawJson=string.Empty;
if(File.Exists(绝对路径))
{
_fileInfo=新的fileInfo(绝对路径);
if(!JsonParser.LoadJsonFile(绝对路径,参考l_rawJson))
{
Log.Error(“无法提取Json文件数据”);
返回;
}
//用清单数据填充当前类
PopulateObject(l_rawJson,this);
}
else Log.Warning(“给定的清单文件不存在:\n\r{0}”,绝对路径);
}
}
}
ManifestDescriptor包含一个文档属性,实现如下:
使用Newtonsoft.Json;
使用制度;
使用System.IO;
使用System.Linq;
命名空间BaseService.Manifest
{
///
///提供Json文档版本的类容器及其实用程序
///
公营文件组
{
私人版(u版),;
私有日期时间\u lastUpdate;
私有字符串描述;
私有字符串_id;
私有字符串_类;
///
///包含文档版本
///
[JsonProperty(“版本”)]
公开版本
{
get=>\u版本;
内部集
{
如果(值!=\u版本)
{
_版本=价值;
}
}
}
///
///包含最新的更新文档版本
///
[JsonProperty(“LastUpdate”)]
公共日期时间最后更新
{
get=>\u lastUpdate;
内部集
{
如果(值!=\u上次更新)
{
_lastUpdate=值;
}
}
}
///
///包含文档描述
///
[JsonProperty(“描述”)]
公共字符串描述
{
获取=>\u描述;
内部集
{
如果(值!=\u说明)
{
_描述=值;
}
}
}
///
///包含文档ID
///
[JsonProperty(“Id”)]
公共字符串Id
{
get=>\u id;
内部集
{
如果(值!=\u id)
{
_id=值;
}
}
}
///
///要实例化文档容量数据的对象类类型
///
[JsonIgnore]
[JsonProperty(“类别”)]
公共字符串类
{
get=>\u类;
内部集
{
如果(值!=\u类)
{
_类别=价值;
}
}
}
}
}
但是,当到达行JsonConvert.PopulateObject(l_rawJson,this)时代码>在ManifestDescriptor类构造函数中调用,我将到达以下执行选项:
无法将JSON对象填充到类型
“BaseService.Manifest.ManifestDescriptor”。路径“文档”,第2行,
立场13。”
您能了解这个问题吗?
感谢您的关注,根据@SelimYildiz,您确实需要将对象的填充分离到您试图序列化的对象之外。
如果您打算序列化您的ManifestDescriptor
,而不知道它的过程,那么您所需要的只是通过json。然后您可以创建一个包装类来完成这项工作。
您打算让ManifestDescriptor
继承一个JObject
?
这可能会给您一个提示,对于这个示例演示,我没有继承JObject
,而是继承jsonvert.PopulateObject
I u
public class ParserWrapper<T>
where T : new()
{
private FileInfo _fileInfo;
public T ParsedObject { get; set; }
public ParserWrapper(string absolutePath)
{
string l_rawJson = string.Empty;
if (File.Exists(absolutePath))
{
_fileInfo = new FileInfo(absolutePath);
if (!JsonParser.LoadJsonFile(absolutePath, ref l_rawJson))
{
System.Diagnostics.Debug.Write("Could not extract Json file data.");
return;
}
// Populate current class with manifest data
ParsedObject = JsonConvert.DeserializeObject<T>(l_rawJson);
}
else System.Diagnostics.Debug.Write("Given manifest file do not exist: \n\r {0}", absolutePath);
}
}
public class ManifestDescriptor
{
private DocSection _document;
private List<Capacity> _capacity;
[JsonProperty("Document")]
public DocSection Document
{
get => _document;
set
{
if (value != _document)
{
_document = value;
}
}
}
[JsonProperty("Capacities")]
public List<Capacity> Capacities
{
get => _capacity;
internal set
{
if (value != _capacity)
{
_capacity = value;
}
}
}
}
var obj = new ParserWrapper<ManifestDescriptor>("json1.json");
public class ManifestDescriptor
{
/// <summary>
/// Manifest file information field
/// </summary>
private FileInfo _fileInfo;
/// <summary>
/// Contains Json document descriptor data
/// </summary>
private DocSection _document;
private List<Capacity> _capacity;
/// <summary>
/// Manifest file information
/// </summary>
public FileInfo FileInfo
{
get => _fileInfo;
set
{
if(value != _fileInfo)
_fileInfo = value;
}
}
/// <summary>
/// Contains Json document descriptor data
/// </summary>
[JsonProperty("Document")]
public DocSection Document
{
get => _document;
set
{
if (value != _document)
{
_document = value;
}
}
}
/// <summary>
/// Contains document capacities
/// </summary>
[JsonProperty("Capacities")]
public List<Capacity> Capacities
{
get => _capacity;
internal set
{
if (value != _capacity)
{
_capacity = value;
}
}
}
public ManifestDescriptor(string absolutePath)
{
string l_rawJson = string.Empty;
if (File.Exists(absolutePath))
{
_fileInfo = new FileInfo(absolutePath);
if (!JsonParser.LoadJsonFile(absolutePath, ref l_rawJson))
{
Log.Error("Could not extract Json file data.");
return;
}
// Populate current class with manifest data
JsonConvert.PopulateObject(l_rawJson, this);
}
else Log.Warning("Given manifest file do not exist: \n\r {0}", absolutePath);
}
}
/// <summary>
/// Class container giving Json document version & its utility
/// </summary>
public class DocSection
{
private Version _version;
private DateTime _lastUpdate;
private string _description;
private string _id;
private string _class;
/// <summary>
/// Contains document version
/// </summary>
[JsonProperty("Version")]
public Version Version
{
get => _version;
set
{
if (value != _version)
{
_version = value;
}
}
}
/// <summary>
/// Contains last update document version
/// </summary>
[JsonProperty("LastUpdate")]
public DateTime LastUpdate
{
get => _lastUpdate;
set
{
if(value != _lastUpdate)
{
_lastUpdate = value;
}
}
}
/// <summary>
/// Contains document description
/// </summary>
[JsonProperty("Description")]
public string Description
{
get => _description;
set
{
if(value != _description)
{
_description = value;
}
}
}
/// <summary>
/// Contains document ID
/// </summary>
[JsonProperty("Id")]
public string Id
{
get => _id;
internal set
{
if (value != _id)
{
_id = value;
}
}
}
}