class MyConverter : CustomCreationConverter<IDictionary<string, object>>
{
public override IDictionary<string, object> Create(Type objectType)
{
return new Dictionary<string, object>();
}
public override bool CanConvert(Type objectType)
{
// in addition to handling IDictionary<string, object>
// we want to handle the deserialization of dict value
// which is of type object
return objectType == typeof(object) || base.CanConvert(objectType);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.StartObject
|| reader.TokenType == JsonToken.Null)
return base.ReadJson(reader, objectType, existingValue, serializer);
// if the next token is not an object
// then fall back on standard deserializer (strings, numbers etc.)
return serializer.Deserialize(reader);
}
}
class Program
{
static void Main(string[] args)
{
var json = File.ReadAllText(@"c:\test.json");
var obj = JsonConvert.DeserializeObject<IDictionary<string, object>>(
json, new JsonConverter[] {new MyConverter()});
}
}
var jsonObject = JsonConvert.DeserializeObject(requestString);
var apiRequest = ToApiRequest(jsonObject);
// call the legacy C++ API ...
下面是我的方法,它可以转换为API所期望的结构:
private static object ToApiRequest(object requestObject)
{
switch (requestObject)
{
case JObject jObject: // objects become Dictionary<string,object>
return ((IEnumerable<KeyValuePair<string, JToken>>) jObject).ToDictionary(j => j.Key, j => ToApiRequest(j.Value));
case JArray jArray: // arrays become List<object>
return jArray.Select(ToApiRequest).ToList();
case JValue jValue: // values just become the value
return jValue.Value;
default: // don't know what to do here
throw new Exception($"Unsupported type: {requestObject.GetType()}");
}
}
//does NOT work (newtonDeserialized does not have the same data in the nested Dictionaries as in object):
var newtonSerialized = Newtonsoft.Json.JsonConvert.SerializeObject(object);
var newtonDeserialized = Newtonsoft.Json.JsonConvert.DeserializeObject<WaitlistResponse>(newtonSerialized);
//Works (netDeserialized have the same data in the nested Directories as in object):
var netSerialized = System.Text.Json.JsonSerializer.Serialize(object);
var netDeserialized = System.Text.Json.JsonSerializer.Deserialize<WaitlistResponse>(netSerialized);
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.StartObject || reader.TokenType == JsonToken.Null)
return base.ReadJson(reader, objectType, existingValue, serializer);
//if it's an array serialize it as a list of dictionaries
if(reader.TokenType == JsonToken.ArrayStart)
return serializer.Deserialize(reader, typeof(List<Dictionary<string, object>>));
// if the next token is not an object
// then fall back on standard deserializer (strings, numbers etc.)
return serializer.Deserialize(reader);
}
public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue,
JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.StartObject || reader.TokenType == JsonToken.Null)
return base.ReadJson(reader, objectType, existingValue, serializer);
//if it's an array serialize it as a list of dictionaries
if (reader.TokenType == JsonToken.StartArray)
{
return serializer.Deserialize(reader, typeof(List<object>)); // instead of List<Dictionary<string, object>>
}
// if the next token is not an object
// then fall back on standard deserializer (strings, numbers etc.)
return serializer.Deserialize(reader);
}
var settings = new JsonSerializerSettings { TypeNameHandling= TypeNameHandling.All };
var serializeValues = JsonConvert.SerializeObject(nestedDictionaryValues, settings);
//After serialize using deserialize using the same TypeNameHandling.
var deserializeValues = JsonConvert.DeserializeObject(serializeValues, settings);
7条答案
按热度按时间jaxagkaj1#
我发现了一种通过提供
CustomCreationConverter
实现将所有嵌套对象转换为Dictionary<string,object>
的方法:文件:* * 一个
wb1gzix02#
当我遇到这个问题时,我有一个非常相似但稍微复杂一些的需求。起初我想也许我可以修改公认的答案,但这似乎有点复杂,我最终采取了一种不同的方法。我试图将一个现代的JSON层放在一个遗留的C++ API之上。我不想给你讲细节,只想说需求归结为:
Dictionary<string,object>
。List<object>
。我首先将请求字符串反序列化为一个Newtonsoft JSON对象,然后调用我的方法根据上述要求进行转换:
下面是我的方法,它可以转换为API所期望的结构:
我希望有人能发现这种方法是有用的。
6qqygrtg3#
替代/更新:
我需要反序列化
String
s的字典的字典,并且使用当前的Json .NET(5.0),我不必创建CustomConverter,我只使用(在VB .NET中):或者,在C#中:
dba5bblo4#
我有一个嵌套/深结构的“未知”字典是序列化/反序列化到/从C#对象和JSON字符串。
如果我使用
Newtonsoft
,它不会自动工作。如果我使用
System.Text.Json
,它会自动工作。wpx232ag5#
@AlexD的公认解决方案在json中有数组的情况下不能理想地工作,它返回
JObject
的JArray
,而不是List<Dictionary<string, object>>
这可以通过修改ReadJson()方法来解决:
cnwbcb6i6#
在我的例子中,并不是所有的东西都是嵌套字典,我还有一个基元类型的key-value数组,当数组的对象不是字典时,它抛出异常。
根据菲利普的回答我得出了
希望对那些还没开始工作的人有帮助。
at0kjp5o7#
首先,您需要序列化字典值。