在 Visual Studio 中反序列化 JSON 文件时出错

Posted

技术标签:

【中文标题】在 Visual Studio 中反序列化 JSON 文件时出错【英文标题】:Error deserializing JSON file in Visual Studio 【发布时间】:2020-06-09 15:53:45 【问题描述】:

我在尝试从以下 JSON 表中提取数据时遇到困难:

    [
    "type":"header","version":"4.8.3","comment":"Export to JSON plugin for phpMyAdmin",
    "type":"database","name":"archaism_dictionary",
    "type":"table","name":"dictionary","database":"archaism_dictionary","data":
    [
    "id":"0","word":"wordOne","synonym":null,"definition":"defOne",
    "id":"1","word":"wortTwo","synonym":null,"definition":"defTwo"
    ]
    
    ]

我的目标是为每个“单词”和每个“定义”获取一个字符串输出。我有以下对应于 JSON 文件的类:

public class Rootobject

    public Class1[] Property1  get; set; 


public class Class1

    public string type  get; set; 
    public string version  get; set; 
    public string comment  get; set; 
    public string name  get; set; 
    public string database  get; set; 
    public Datum[] data  get; set; 


public class Datum

    public string id  get; set; 
    public string word  get; set; 
    public object synonym  get; set; 
    public string definition  get; set; 

最后这段代码应该是从字符串结果中的表中检索第一个单词:

var list = JsonConvert.DeserializeObject<List<Dictionary.Rootobject>>(rawJSON);
string result = list[0].Property1[0].data[0].word;

.Property[0] 返回 null 并且程序给了我一个 null 引用异常。我的代码哪里出错了,我应该如何完成这项任务?谢谢。

编辑: 我不确定这是否会弄乱 rawJSON 字符串,但我是这样理解的:

rawJSON = File.ReadAllText(FileSystem.AppDataDirectory + fileName);

【问题讨论】:

能否提供一个完整有效的json内容?否则很难找出问题所在。 数据中的前两个元素没有“数据”属性。你真的检查过反序列化的 json 吗? 您可以在 Visual Studio 中复制您的 json 并选择性粘贴以创建适当的类,或使用 www.json2csharp.com 站点查看正确反序列化 json 所需的类 请查看minimal reproducible example 发布代码指南并相应地发布edit。特别要确保提供准确(但最少)的 JSON 来证明问题。此外,由于它是 NRE,请务必查看 ***.com/questions/4660142/…。 我很乐意提供帮助,但如果没有有效的 json 示例,我将无法提供帮助。 【参考方案1】:

@Claudio Valerio 提供正确的 json 数据。

根据我的测试,您可以尝试下面的代码来获取列表中的word

Json 大雅:


"Property1":[
"type":"header","version":"4.8.3","comment":"Export to JSON plugin for PHPMyAdmin",
"type":"database","name":"archaism_dictionary",
"type":"table","name":"dictionary","database":"archaism_dictionary","data":
  [
    "id":"0","word":"wordOne","synonym":null,"definition":"defOne",
    "id":"1","word":"wortTwo","synonym":null,"definition":"defTwo"
  ]
  
 ]

来自 JSON 数据的类:

 public class Rootobject

    public Property1[] Property1  get; set; 


public class Property1

    public string type  get; set; 
    public string version  get; set; 
    public string comment  get; set; 
    public string name  get; set; 
    public string database  get; set; 
    public Datum[] data  get; set; 


public class Datum

    public string id  get; set; 
    public string word  get; set; 
    public object synonym  get; set; 
    public string definition  get; set; 

代码:

  string rawJSON = @"
  'Property1':[
    'type':'header','version':'4.8.3','comment':'Export to JSON plugin for PHPMyAdmin',
   'type':'database','name':'archaism_dictionary',
   'type':'table','name':'dictionary','database':'archaism_dictionary','data':
   [
    'id':'0','word':'wordOne','synonym':null,'definition':'defOne',
     'id':'1','word':'wortTwo','synonym':null,'definition':'defTwo'
  ]
  
 ]
";
        var list = JsonConvert.DeserializeObject<Rootobject>(rawJSON);
        string result = list.Property1[2].data[0].word;

【讨论】:

【参考方案2】:

您的输入 json 更有可能是这样的:

[
"type":"header","version":"4.8.3","comment":"Export to JSON plugin for PHPMyAdmin",
"type":"database","name":"archaism_dictionary",
"type":"table","name":"dictionary","database":"archaism_dictionary","data":
[
"id":"0","word":"wordOne","synonym":null,"definition":"defOne",
"id":"1","word":"wortTwo","synonym":null,"definition":"defTwo"
]

注意方括号,它们很重要。

如果我猜对了,你会想像这样反序列化:

var list = JsonConvert.DeserializeObject<List<Class1>>(rawJSON);
string result = list[2].data[0].word;

注意:只有当您的输入 json 为:


  "Property1":[
    "type":"header","version":"4.8.3","comment":"Export to JSON plugin for PHPMyAdmin",
    "type":"database","name":"archaism_dictionary",
    "type":"table","name":"dictionary","database":"archaism_dictionary","data":
      [
        "id":"0","word":"wordOne","synonym":null,"definition":"defOne",
        "id":"1","word":"wortTwo","synonym":null,"definition":"defTwo"
      ]
    
  ]

并使用

var myRoot = JsonConvert.DeserializeObject<RootObject>(rawJSON);
string result = myRoot.Property1[2].data[0].word;

【讨论】:

谢谢,我用你的(第一个)和你的命令替换了 JSON,但我仍然得到 null 作为返回值。 索引器中有错字,它是 list[2] 而不是 list[0]。我编辑了答案并为 secpnd json 添加了代码。试一试。【参考方案3】:

你需要空句柄(json NullValueHandling)下面是我的代码请看一下:

string stringJson = @"
                              'Property1':[
                                'type':'header','version':'4.8.3','comment':'Export to JSON plugin for PHPMyAdmin',
                               'type':'database','name':'archaism_dictionary',
                               'type':'table','name':'dictionary','database':'archaism_dictionary','data':
                               [
                                'id':'0','word':'wordOne','synonym':null,'definition':'defOne',
                                 'id':'1','word':'wortTwo','synonym':null,'definition':'defTwo'
                              ]
                              
                             ]
                            ";

        try
        
            var settings = new JsonSerializerSettings
            
                NullValueHandling = NullValueHandling.Ignore,
                MissingMemberHandling = MissingMemberHandling.Ignore
            ;
            var list = JsonConvert.DeserializeObject<BaseResponse>(stringJson,settings);
            string result = list.Property1[2].data[0].word;
        
        catch (Exception ex)
        
            Console.WriteLine(ex.Message);
        

型号:

public class WordData

    public string id  get; set; 
    public string word  get; set; 
    public object synonym  get; set; 
    public string definition  get; set; 


public class PropertyData

    public string type  get; set; 
    public string version  get; set; 
    public string comment  get; set; 
    public string name  get; set; 
    public string database  get; set; 
    public List<WordData> data  get; set; 


public class BaseResponse

    public List<PropertyData> Property1  get; set; 

希望对你有帮助

谢谢

【讨论】:

以上是关于在 Visual Studio 中反序列化 JSON 文件时出错的主要内容,如果未能解决你的问题,请参考以下文章

在 Visual Basic .NET 中反序列化 JSON

在Visual Basic .NET中反序列化JSON

Visual Studio 怎么反汇编

在android studio中反序列化操作wcf的请求消息体时出错

visual studio 2010 序列号在哪输入

Visual Studio 2019 密钥 Visual Studio 2019序列号 Visual Studio 2019 永久激活工具