Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用动态属性名反序列化json_Json_Json.net_Json Deserialization - Fatal编程技术网

使用动态属性名反序列化json

使用动态属性名反序列化json,json,json.net,json-deserialization,Json,Json.net,Json Deserialization,这里也提出了类似的问题。我无法使其在以下情况下工作: 我有下面的JSON { "notes": { "data": [{ "book": { "items": [{ "page": "page 1", "comment": "Some notes"

这里也提出了类似的问题。我无法使其在以下情况下工作:

我有下面的JSON

{
    "notes": {
        "data": [{
                "book": {
                    "items": [{
                            "page": "page 1",
                            "comment": "Some notes"
                        },
                        {
                            "page": "page 1",
                            "comment": "Some notes"
                        }
                    ]
                }
            },
            {
                "journal": {
                    "items": [{
                            "page": "page 1",
                            "comment": "Some notes"
                        },
                        {
                            "page": "page 1",
                            "comment": "Some notes"
                        }
                    ]
                }
            },
            {
                "magazine": {
                    "items": [{
                            "page": "page 1",
                            "comment": "Some notes"
                        },
                        {
                            "page": "page 1",
                            "comment": "Some notes"
                        }
                    ]
                }
            }
        ]
    }
}
为JSON序列化创建了以下类:

public class TestParse
  {
    public Note Notes { get; set; }
  }
  public class Note
  {
    public IList<Data> Data { get; set; }
  }

  public class Data
  {
    public Source Source { get; set; }
  }

  [JsonConverter(typeof(MyConverter))]
  public class Source
  {
    public IList<Items> Items { get; set; }
  }

  public class Items
  {
    public string Page { get; set; }
    public string Comment { get; set; }
  }

  public class MyConverter : JsonConverter
  {
    public override bool CanConvert(Type objectType)
    {
      return objectType == typeof(Source);
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue,
        JsonSerializer serializer)
    {
      var jo = JObject.Load(reader);

      var req = new Data
      {
        Source = jo.ToObject<Source>()
      };
      return req;
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
      var req = (Source)value;
      var jo = new JObject(
          new JObject(req.Items));
      jo.WriteTo(writer);
    }
  }
}
公共类TestParse
{
公共注释{get;set;}
}
公开课堂讲稿
{
公共IList数据{get;set;}
}
公共类数据
{
公共源{get;set;}
}
[JsonConverter(类型(MyConverter))]
公共类源
{
公共IList项{get;set;}
}
公共类项目
{
公共字符串页{get;set;}
公共字符串注释{get;set;}
}
公共类MyConverter:JsonConverter
{
公共覆盖布尔CanConvert(类型objectType)
{
返回objectType==typeof(源);
}
public override object ReadJson(JsonReader reader,Type objectType,object existingValue,
JsonSerializer(序列化程序)
{
var jo=JObject.Load(读卡器);
var req=新数据
{
Source=jo.ToObject()
};
返回请求;
}
公共重写void WriteJson(JsonWriter编写器、对象值、JsonSerializer序列化器)
{
var req=(源)值;
var jo=新作业对象(
新作业对象(需求项目);
写作工作(作家);
}
}
}
我仍然无法反序列化“源代码”。任何指针都是值得赞赏的

在您引用的中,动态键由同一对象中另一个属性的值确定。因此,需要一个转换器

在您的情况下,您有动态关键点,但它们不依赖于任何东西。 在这里,你并不真的需要转换器;您可以使用字典来处理动态键

注释
类中更改此行:

public IList<Data> Data { get; set; }
public IList数据{get;set;}
为此:

public IList<IDictionary<string, Source>> Data { get; set; }
public IList数据{get;set;}
并从
类中删除
[JsonConverter]
属性。那么它应该会起作用。
(您也可以删除
数据
类,因为它不是必需的。)


Fiddle:

上面哪些属性名称是动态的?具体处理的JSON格式为
{“type”:“some name”,“some name”:“value”}
,看起来与您的JSON完全不匹配。@dbc。在上面的示例中,动态特性/对象是书本、日记和杂志。我把银行、银行卡和这些作品等同起来,就像一个查理·布莱恩,这个案子怎么样?会话id属性名称为动态{“errors”:{“会话id”:[“不允许空值”]}}@Gumbo
公共类根{public Dictionary errors{get;set;}}