gpt4 book ai didi

javascript - 将 JSON 数据反序列化到字典 时出错

转载 作者:行者123 更新时间:2023-11-28 08:28:49 27 4
gpt4 key购买 nike

这是我想要使用 native Javascript 支持反序列化为字典的 JSON。

string data = "{"Symptom":[true,true,true],"Action":[true,true],"AllArea":true}";

但是当我尝试使用下面的代码反序列化时

字典值 = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize>(data);

它给了我一个错误说明“数组的反序列化不支持类型‘System.String’”

我正在使用.Net Framework 3.5。请帮我完成这件事。

最佳答案

我猜你不能直接将其转换为字典...我认为反序列化器需要一个相应的类型,具有可理解的属性名称和类型,

我认为你可以转换为类型,然后生成你的字典,例如:

public class MyClass
{
public List<bool> Symptom { get; set; }
public List<bool> Action { get; set; }
public bool AllArea { get; set; }

public Dictionary<string, List<bool>> getDic()
{
// this is for example, and many many different may be implement
// maybe some `reflection` for add property dynamically or ...

var oDic = new Dictionary<string, List<bool>>();
oDic.Add("Symptom", this.Symptom);
oDic.Add("Action", this.Action);
oDic.Add("AllArea", new List<bool>() { AllArea });
return oDic;
}
}

然后:

string data = "{\"Symptom\":[true,true,true],\"Action\":[true,true],\"AllArea\":true}";
System.Web.Script.Serialization.JavaScriptSerializer aa = new System.Web.Script.Serialization.JavaScriptSerializer();
var o = aa.Deserialize<MyClass>(data);
var dic = o.getDic();

无论如何,这是个好问题

关于javascript - 将 JSON 数据反序列化到字典 <string, string> 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22090090/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com