简体   繁体   English

json 字符串的反序列化给出 null 值

[英]Deserialization of a json string gives null value

{
  "apple": {
    "A": "xyz",
    "B": "abc",
    "C": "jkl"
  },
  "banana": {
    "A": "lotus",
    "B": "oil",
    "C": "cat"
  }
}

This is my JSON and below is my model class where I want to map the JSON data.这是我的 JSON 下面是我的 model class 我想要 map JSON 数据。

 public class Wrapper
    {
        public Dictionary<string, item> fruits{ get; set; }
    }
public class item
    {
        public string A{get; set;}
        public string B{get; set;}
        public string C{get; set;}
    }

when I am using the following code to deserialize the Json string I am getting null as response.当我使用以下代码反序列化 Json 字符串时,我得到 null 作为响应。

 var value=Newtonsoft.Json.JsonConvert.DeserializeObject<Wrapper>(jsonString);

you don't need any wrapper你不需要任何包装

var value=Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, item>>(jsonString);

Try wrap in an element fruits :尝试包装一个元素fruits

{
  "fruits":
  {
    "apple": {
      "A": "xyz",
      "B": "abc",
      "C": "jkl"
    },
    "banana": {
      "A": "lotus",
      "B": "oil",
      "C": "cat"
    }
  } 
}

To validate that your input is correct - instantiate an instance of the wrapper class and serialise it - then you can compare that your input matches the structure.要验证您的输入是否正确 - 实例化包装器 class 的实例并将其序列化 - 然后您可以比较您的输入是否与结构匹配。

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

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