c# - C# JsonConvert.DeserializeObject 没有正确填充对象
问题描述
JsonConvert.DeserializeObject<Language>(json)
返回属性为空的对象。我想填充 lang 对象,它应该有一些属性,例如名为“IncreaseIndent”的字符串列表等。
缩进器.json
{"VBA":[
{ "IncreaseIndent":["Public Sub *","Public Function *","If *then","While *","Do","Do while *","For *","With *","Select Case *","Sub *","Function *","* Type *","Private Sub *","Private Function *","* Enum *"]},
{ "DecreaseIndent":["End Sub","End Function","End if","Wend","Loop While *","Loop","Next","Next *","End With","End Select","End Type","End Enum"] },
{ "Exception":["Elseif *","Else","Case *","Case"]}
]}
C#
public static string FormatCode(string strCode)
{
Language lang;
using (StreamReader r = new StreamReader("A:/standardvba/Logic/Indentor.json"))
{
string json = r.ReadToEnd();
lang = JsonConvert.DeserializeObject<Language>(json); //Inside lang every property is null
return IndentCodePiece(strCode, lang);
}
}
public class Language
{
public string ProgrammingLanguage { get; set; }
public List<string> IncreaseIndent { get; set; }
public List<string> DecreaseIndent { get; set; }
public List<string> Exception { get; set; }
}
解决方案
您的JSON 格式错误,IncreaseIndent
应该是字符串列表:DecreaseIndent
Exception
{"VBA":[
{ "IncreaseIndent":["Public Sub *","Public Function *",...] },
{ "DecreaseIndent":["End Sub","End Function", ...] },
{ "Exception":["Elseif *","Else","Case *","Case"] }
]}
我在每个值的末尾添加了[在 II、DI 和 Ex 之后并添加了] !
您可以在页面https://jsonlint.com上验证 josn ,您会看到验证将失败。如果您引入更改我建议验证是可以的!
推荐阅读
- python - 尝试在 Python 的文本文件中搜索数字并检查它是否存在
- javascript - 在 laravel 7 中登录后出现 500 服务器错误
- go - 有没有办法使用 Go 获取带有电子邮件和密码的 Google OAuth2 令牌?
- c# - 在图像标签中使用cshtml?
- sql - 如何使用第一个表中的逗号分隔值从另一个表中选择行?
- dlib - simple_object_detector 的输出是什么类型的文件?
- cmake - 编译 Cartographer CMake 错误找不到包配置... cartographer_ros_msgsConfig.cmake
- javascript - Webpack 从不同的块导入模块,而不是同一块中可用的最新版本
- linux - 打开已打开配置文件的新 Firefox 窗口 | 重击,Linux
- azure-devops - 如何在 Azure devops 管道中的 yaml 任务上指定 Cake.Tool 版本