使用 JSON.Net 反序列化器反序列化动态 JSON
Deserializing dynamic JSON using JSON.Net deserializer
我正在尝试扩展此处给出的 JSON.net 示例
尝试遵循 JSON.net 中的示例
http://james.newtonking.com/projects/json/help/CustomCreationConverter.html 反序列化对象。
Class结构:
public class Rootobject
{
public int took { get; set; }
public bool timed_out { get; set; }
public _Shards _shards { get; set; }
public Hits hits { get; set; }
}
public class _Shards
{
public int total { get; set; }
public int successful { get; set; }
public int failed { get; set; }
}
public class Hits
{
public int total { get; set; }
public float max_score { get; set; }
public Hit[] hits { get; set; }
}
public class Hit
{
public string _index { get; set; }
public string _type { get; set; }
public string _id { get; set; }
public float _score { get; set; }
public Inner_Hits inner_hits { get; set; }
}
public class Inner_Hits
{
public Testp11 testp11 { get; set; }
public Testp10 testp10 { get; set; }
}
public class Testp11
{
public Hits1 hits { get; set; }
}
public class Hits1
{
public int total { get; set; }
public float max_score { get; set; }
public Hit1[] hits { get; set; }
}
public class Hit1
{
public string _index { get; set; }
public string _type { get; set; }
public string _id { get; set; }
public _Nested _nested { get; set; }
public float _score { get; set; }
public _Source _source { get; set; }
}
public class _Nested
{
public string field { get; set; }
public int offset { get; set; }
public _Nested1 _nested { get; set; }
}
public class _Nested1
{
public string field { get; set; }
public int offset { get; set; }
public _Nested2 _nested { get; set; }
}
public class _Nested2
{
public string field { get; set; }
public int offset { get; set; }
}
public class _Source
{
public string pname { get; set; }
public Value[] values { get; set; }
}
public class Value
{
public string pid { get; set; }
public string[] val { get; set; }
}
public class Testp10
{
public Hits2 hits { get; set; }
}
public class Hits2
{
public int total { get; set; }
public float max_score { get; set; }
public Hit2[] hits { get; set; }
}
public class Hit2
{
public string _index { get; set; }
public string _type { get; set; }
public string _id { get; set; }
public _Nested3 _nested { get; set; }
public float _score { get; set; }
public _Source1 _source { get; set; }
}
public class _Nested3
{
public string field { get; set; }
public int offset { get; set; }
public _Nested4 _nested { get; set; }
}
public class _Nested4
{
public string field { get; set; }
public int offset { get; set; }
public _Nested5 _nested { get; set; }
}
public class _Nested5
{
public string field { get; set; }
public int offset { get; set; }
}
public class _Source1
{
public string pname { get; set; }
public Value1[] values { get; set; }
}
public class Value1
{
public string pid { get; set; }
public string[] val { get; set; }
}
在我的 json 文档调用函数中:
string json = "{ \"took\": 2, \"timed_out\": false, \"_shards\": { \"total\": 5, \"successful\": 5, \"failed\": 0 }, \"hits\": { \"total\": 4, \"max_score\": 8.225408, \"hits\": [ { \"_index\": \"dev28\", \"_type\": \"Index_Type1\", \"_id\": \"user_2\", \"_score\": 8.225408, \"inner_hits\": { \"testp11\": { \"hits\": { \"total\": 1, \"max_score\": 5.816241, \"hits\": [ { \"_index\": \"dev28\", \"_type\": \"Index_Type1\", \"_id\": \"user_2\", \"_nested\": { \"field\": \"doc\",\"offset\": 0, \"_nested\": { \"field\": \"roles\", \"offset\": 0, \"_nested\": { \"field\": \"permissions\", \"offset\": 0 } } }, \"_score\": 5.816241, \"_source\": { \"pname\": \"Permission1\", \"values\": [ { \"pid\": \"S1_P1\", \"val\": [ \"V12\", \"V11\" ] } ] } } ] } },\"testp10\": { \"hits\": { \"total\": 1, \"max_score\": 5.816241, \"hits\": [ { \"_index\": \"dev28\", \"_type\": \"Index_Type1\",\"_id\": \"user_2\", \"_nested\": { \"field\": \"doc\", \"offset\": 0, \"_nested\": {\"field\": \"roles\", \"offset\": 0, \"_nested\": { \"field\": \"permissions\", \"offset\": 0}} }, \"_score\": 5.816241, \"_source\": {\"pname\": \"Permission2\",\"values\": [ {\"pid\": \"S1_P1\", \"val\": [\"V12\",\"V11\" ]} ] } } ] } } } }]} }";
dynamic objResultElasticData = (Rootobject)Newtonsoft.Json.JsonConvert.DeserializeObject(json, typeof(Rootobject));
在上面的 classes 中,我有一个 class 作为 "public class Inner_Hits",其中输入数据可以是动态的,具有不同的名称。目前它有 "Testp11" 和 "Testp10",也可以有 "Testp12" 与 "Testp11" 具有相同的层次结构,有人可以给我一个指示,我怎样才能实现这种动态行为。
希望有人能指出我正确的方向。
此致,
杰克
如果您的 class 属性正在动态变化,那么我认为您不能使用此解析器。
看看这个。这可能对你有帮助。
DynamicJSon Parser
您可以利用 Json.NET 的能力 serialize a dictionary 到 JSON name/value 对对象来定义您的 inner_hits
如下:
public Dictionary<string, InnerHit> inner_hits { get; set; }
您还应该组合自动代码生成器创建的各种重复 类。完整的实施将是:
public class Shards
{
public int total { get; set; }
public int successful { get; set; }
public int failed { get; set; }
}
public class Nested
{
public string field { get; set; }
public int offset { get; set; }
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)] // Do not write the null value
public Nested _nested { get; set; }
}
public class Value
{
public string pid { get; set; }
public List<string> val { get; set; }
}
public class Source
{
public string pname { get; set; }
public List<Value> values { get; set; }
}
public class Hit2
{
public string _index { get; set; }
public string _type { get; set; }
public string _id { get; set; }
public Nested _nested { get; set; }
public double _score { get; set; }
public Source _source { get; set; }
}
public class Hits2
{
public int total { get; set; }
public double max_score { get; set; }
public List<Hit2> hits { get; set; }
}
public class InnerHits
{
public Hits2 hits { get; set; }
}
public class Hit
{
public string _index { get; set; }
public string _type { get; set; }
public string _id { get; set; }
public double _score { get; set; }
public Dictionary<string, InnerHits> inner_hits { get; set; }
}
public class Hits
{
public int total { get; set; }
public double max_score { get; set; }
public List<Hit> hits { get; set; }
}
public class RootObject
{
public int took { get; set; }
public bool timed_out { get; set; }
public Shards _shards { get; set; }
public Hits hits { get; set; }
}
另外,这里不需要使用dynamic
,你可以这样做:
var root = JsonConvert.DeserializeObject<RootObject>(json);
并获得静态检查正确性的优势。
我正在尝试扩展此处给出的 JSON.net 示例 尝试遵循 JSON.net 中的示例 http://james.newtonking.com/projects/json/help/CustomCreationConverter.html 反序列化对象。
Class结构:
public class Rootobject
{
public int took { get; set; }
public bool timed_out { get; set; }
public _Shards _shards { get; set; }
public Hits hits { get; set; }
}
public class _Shards
{
public int total { get; set; }
public int successful { get; set; }
public int failed { get; set; }
}
public class Hits
{
public int total { get; set; }
public float max_score { get; set; }
public Hit[] hits { get; set; }
}
public class Hit
{
public string _index { get; set; }
public string _type { get; set; }
public string _id { get; set; }
public float _score { get; set; }
public Inner_Hits inner_hits { get; set; }
}
public class Inner_Hits
{
public Testp11 testp11 { get; set; }
public Testp10 testp10 { get; set; }
}
public class Testp11
{
public Hits1 hits { get; set; }
}
public class Hits1
{
public int total { get; set; }
public float max_score { get; set; }
public Hit1[] hits { get; set; }
}
public class Hit1
{
public string _index { get; set; }
public string _type { get; set; }
public string _id { get; set; }
public _Nested _nested { get; set; }
public float _score { get; set; }
public _Source _source { get; set; }
}
public class _Nested
{
public string field { get; set; }
public int offset { get; set; }
public _Nested1 _nested { get; set; }
}
public class _Nested1
{
public string field { get; set; }
public int offset { get; set; }
public _Nested2 _nested { get; set; }
}
public class _Nested2
{
public string field { get; set; }
public int offset { get; set; }
}
public class _Source
{
public string pname { get; set; }
public Value[] values { get; set; }
}
public class Value
{
public string pid { get; set; }
public string[] val { get; set; }
}
public class Testp10
{
public Hits2 hits { get; set; }
}
public class Hits2
{
public int total { get; set; }
public float max_score { get; set; }
public Hit2[] hits { get; set; }
}
public class Hit2
{
public string _index { get; set; }
public string _type { get; set; }
public string _id { get; set; }
public _Nested3 _nested { get; set; }
public float _score { get; set; }
public _Source1 _source { get; set; }
}
public class _Nested3
{
public string field { get; set; }
public int offset { get; set; }
public _Nested4 _nested { get; set; }
}
public class _Nested4
{
public string field { get; set; }
public int offset { get; set; }
public _Nested5 _nested { get; set; }
}
public class _Nested5
{
public string field { get; set; }
public int offset { get; set; }
}
public class _Source1
{
public string pname { get; set; }
public Value1[] values { get; set; }
}
public class Value1
{
public string pid { get; set; }
public string[] val { get; set; }
}
在我的 json 文档调用函数中:
string json = "{ \"took\": 2, \"timed_out\": false, \"_shards\": { \"total\": 5, \"successful\": 5, \"failed\": 0 }, \"hits\": { \"total\": 4, \"max_score\": 8.225408, \"hits\": [ { \"_index\": \"dev28\", \"_type\": \"Index_Type1\", \"_id\": \"user_2\", \"_score\": 8.225408, \"inner_hits\": { \"testp11\": { \"hits\": { \"total\": 1, \"max_score\": 5.816241, \"hits\": [ { \"_index\": \"dev28\", \"_type\": \"Index_Type1\", \"_id\": \"user_2\", \"_nested\": { \"field\": \"doc\",\"offset\": 0, \"_nested\": { \"field\": \"roles\", \"offset\": 0, \"_nested\": { \"field\": \"permissions\", \"offset\": 0 } } }, \"_score\": 5.816241, \"_source\": { \"pname\": \"Permission1\", \"values\": [ { \"pid\": \"S1_P1\", \"val\": [ \"V12\", \"V11\" ] } ] } } ] } },\"testp10\": { \"hits\": { \"total\": 1, \"max_score\": 5.816241, \"hits\": [ { \"_index\": \"dev28\", \"_type\": \"Index_Type1\",\"_id\": \"user_2\", \"_nested\": { \"field\": \"doc\", \"offset\": 0, \"_nested\": {\"field\": \"roles\", \"offset\": 0, \"_nested\": { \"field\": \"permissions\", \"offset\": 0}} }, \"_score\": 5.816241, \"_source\": {\"pname\": \"Permission2\",\"values\": [ {\"pid\": \"S1_P1\", \"val\": [\"V12\",\"V11\" ]} ] } } ] } } } }]} }";
dynamic objResultElasticData = (Rootobject)Newtonsoft.Json.JsonConvert.DeserializeObject(json, typeof(Rootobject));
在上面的 classes 中,我有一个 class 作为 "public class Inner_Hits",其中输入数据可以是动态的,具有不同的名称。目前它有 "Testp11" 和 "Testp10",也可以有 "Testp12" 与 "Testp11" 具有相同的层次结构,有人可以给我一个指示,我怎样才能实现这种动态行为。
希望有人能指出我正确的方向。
此致, 杰克
如果您的 class 属性正在动态变化,那么我认为您不能使用此解析器。 看看这个。这可能对你有帮助。 DynamicJSon Parser
您可以利用 Json.NET 的能力 serialize a dictionary 到 JSON name/value 对对象来定义您的 inner_hits
如下:
public Dictionary<string, InnerHit> inner_hits { get; set; }
您还应该组合自动代码生成器创建的各种重复 类。完整的实施将是:
public class Shards
{
public int total { get; set; }
public int successful { get; set; }
public int failed { get; set; }
}
public class Nested
{
public string field { get; set; }
public int offset { get; set; }
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)] // Do not write the null value
public Nested _nested { get; set; }
}
public class Value
{
public string pid { get; set; }
public List<string> val { get; set; }
}
public class Source
{
public string pname { get; set; }
public List<Value> values { get; set; }
}
public class Hit2
{
public string _index { get; set; }
public string _type { get; set; }
public string _id { get; set; }
public Nested _nested { get; set; }
public double _score { get; set; }
public Source _source { get; set; }
}
public class Hits2
{
public int total { get; set; }
public double max_score { get; set; }
public List<Hit2> hits { get; set; }
}
public class InnerHits
{
public Hits2 hits { get; set; }
}
public class Hit
{
public string _index { get; set; }
public string _type { get; set; }
public string _id { get; set; }
public double _score { get; set; }
public Dictionary<string, InnerHits> inner_hits { get; set; }
}
public class Hits
{
public int total { get; set; }
public double max_score { get; set; }
public List<Hit> hits { get; set; }
}
public class RootObject
{
public int took { get; set; }
public bool timed_out { get; set; }
public Shards _shards { get; set; }
public Hits hits { get; set; }
}
另外,这里不需要使用dynamic
,你可以这样做:
var root = JsonConvert.DeserializeObject<RootObject>(json);
并获得静态检查正确性的优势。