C# 将JSON字符串反序列化到字典<;字符串,对象>;
我有这个字符串:C# 将JSON字符串反序列化到字典<;字符串,对象>;,c#,.net,json,C#,.net,Json,我有这个字符串: [{ "processLevel" : "1" , "segments" : [{ "min" : "0", "max" : "600" }] }] 我正在反序列化对象: object json = jsonSerializer.DeserializeObject(jsonString); 该对象看起来像: object[0] = Key: "processLevel", Value: "1" object[1] = Key: "segments", Value: ...
[{ "processLevel" : "1" , "segments" : [{ "min" : "0", "max" : "600" }] }]
我正在反序列化对象:
object json = jsonSerializer.DeserializeObject(jsonString);
该对象看起来像:
object[0] = Key: "processLevel", Value: "1"
object[1] = Key: "segments", Value: ...
并尝试创建字典:
Dictionary<string, object> dic = json as Dictionary<string, object>;
Dictionary dic=json作为字典;
但是dic
获取null
有什么问题吗?问题是对象不是Dictionary类型或兼容类型,因此不能直接强制转换。我将创建一个自定义对象并使用反序列化
public class DeserializedObject{
public string processLevel{get;set;}
public object segments{get;set}
}
IEnumerable<DeserializedObject> object=jsonSerializer.Deserialize<IEnumerable<DeserializedObject>>(json);
公共类反序列化对象{
公共字符串processLevel{get;set;}
公共对象段{get;set}
}
IEnumerable object=jsonSerializer.Deserialize(json);
for Theas
关键字说明语句表达式as type
与语句表达式is type等效?(type)表达式:(type)null
。如果运行json.GetType()
它将返回System.Object[]
而不是System.Collections.Generic.Dictionary
在类似这样的情况下,我想要反序列化json对象的对象类型很复杂,我使用类似json.NET的API。您可以编写自己的反序列化程序,如下所示:
class DictionaryConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
Throw(new NotImplementedException());
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
// Your code to deserialize the json into a dictionary object.
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
Throw(new NotImplementedException());
}
}
然后可以使用这个序列化程序将json读入字典对象。这是一个。请参阅mridula的答案,了解为什么会得到null。但是,如果您想直接将json字符串转换为字典,您可以尝试以下代码片段
Dictionary<string, object> values =
JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
字典值=
反序列化对象(json);
我喜欢这种方法:
using Newtonsoft.Json.Linq;
//jsonString is your JSON-formatted string
JObject jsonObj = JObject.Parse(jsonString);
Dictionary<string, string> dictObj = jsonObj.ToObject<Dictionary<string, object>>();
使用Newtonsoft.Json.Linq;
//jsonString是JSON格式的字符串
JObject jsonObj=JObject.Parse(jsonString);
Dictionary dictObj=jsonObj.ToObject();
现在,您可以使用
dictObj
作为字典访问任何您想要的内容。如果希望以字符串形式获取值,也可以使用字典。我也遇到了同样的问题,并找到了解决方案
- 很简单
- 没有虫子
- 在可操作产品上测试
步骤1)创建具有2个属性的泛型类
public class CustomDictionary<T1,T2> where T1:class where T2:class
{
public T1 Key { get; set; }
public T2 Value { get; set; }
}
公共类CustomDictionary其中T1:class其中T2:class
{
公共T1密钥{get;set;}
公共T2值{get;set;}
}
步骤2)创建新类并从第一个类继承
public class SectionDictionary: CustomDictionary<FirstPageSectionModel, List<FirstPageContent>>
{
}
public类SectionDictionary:CustomDictionary
{
}
步骤3)替换字典和列表
public Dictionary<FirstPageSectionModel, List<FirstPageContent>> Sections { get; set; }
公共字典节{get;set;}
及
公共列表节{get;set;}
步骤4)轻松地序列化或反序列化
{
firstPageFinal.Sections.Add(new SectionDictionary { Key= section,Value= contents });
var str = JsonConvert.SerializeObject(firstPageFinal);
var obj = JsonConvert.DeserializeObject<FirstPageByPlatformFinalV2>(str);
}
{
添加(新的SectionDictionary{Key=section,Value=contents});
var str=JsonConvert.SerializeObject(firstPageFinal);
var obj=JsonConvert.DeserializeObject(str);
}
非常感谢关于一个相关问题的这个答案有完整的解决方案源代码:感谢您使用
行包含了,太多的示例遗漏了这一点。@Blairg23在上面的示例中应该是'jsonObj.ToObject()`如何将dictObj转换回json格式的字符串?如果“json作为IDictionary”,则var json=JsonConvert.DeserializeObject(jsonString,new ExpandoObjectConverter());//也许ExpandoObject就是你所想的动态对象。
{
firstPageFinal.Sections.Add(new SectionDictionary { Key= section,Value= contents });
var str = JsonConvert.SerializeObject(firstPageFinal);
var obj = JsonConvert.DeserializeObject<FirstPageByPlatformFinalV2>(str);
}