如何解决C# MVC 无法反序列化元组
private class SearchMetadataJson
{
public string entertain { get; set; }
public string master { get; set; }
public string memail { get; set; }
public string key { get; set; }
public (int,string)[] mood { get; set; }
public int? soundnumber { get; set; }
public int? ftv { get; set; }
public int? com { get; set; }
public (int,string)[] sims { get; set; }
public (int,string)[] keysecond { get; set; }
public string popt { get; set; }
public (string,string) syncs { get; set; }
}
我尝试像这样反序列化对象
var CommentObj = JsonSerializer.Deserialize<SearchMetadataJson>(CommentAsstring);
我试图反序列化的数据(又名“CommentAsstring”)看起来像这样
"{\"entertain\":\"PEG\",\"master\":\"Phos Ent Group\",\"memail\":\"example@example.com\",\"key\":\"Db\",\"mood\":{\"1\":\"TypeA\",\"4\":\"TypeB\",\"5\":\"TypeC\"},\"soundnumber\":\"5\",\"ftv\":\"4\",\"com\":\"3\",\"sims\":{\"1\":\"Band1\",\"2\":\"Band2\"},\"keysecond\":{\"1\":\"KeyWord1\",\"2\":\"KeyWord2\",\"3\":\"KeyWord3\"},\"syncs\":{\"Other pubber\":\"example2@example.com\"}}"
但是我一直收到这个错误
有人看到问题出在哪里了吗?
更新CommentAsstring
中的整数是变量,每次调用函数时都会有所不同,因此我无法创建具有特定整数键值的 Json 对象。
解决方法
我们来看看实际的格式化数据结构
{
"entertain":"PEG","master":"Phos Ent Group","memail":"example@example.com","key":"Db","mood":{
"1":"TypeA","4":"TypeB","5":"TypeC"
},"soundnumber":"5","ftv":"4","com":"3","sims":{
"1":"Band1","2":"Band2"
},"keysecond":{
"1":"KeyWord1","2":"KeyWord2","3":"KeyWord3"
},"syncs":{
"Other pubber":"example2@example.com"
}
}
将这些转换为元组的数组是不寻常的。你看似拥有的是字典的
示例
private class SearchMetadataJson
{
public string entertain { get; set; }
public string master { get; set; }
public string memail { get; set; }
public string key { get; set; }
public Dictionary<int,string> mood { get; set; }
public int? soundnumber { get; set; }
public int? ftv { get; set; }
public int? com { get; set; }
public Dictionary<int,string> sims { get; set; }
public Dictionary<int,string> keysecond { get; set; }
public string popt { get; set; }
// public (string,string) syncs { get; set; }
}
最后一个属性是一个对象还是另一个字典是有争议的。
"syncs":{
"Other pubber":"example2@example.com"
}
不过,我会把它留给你。
,您的模型有错误,请使用此站点在 C# 中转换您的 json https://json2csharp.com/
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class Mood {
public string _1 { get; set; }
public string _4 { get; set; }
public string _5 { get; set; }
}
public class Sims {
public string _1 { get; set; }
public string _2 { get; set; }
}
public class Keysecond {
public string _1 { get; set; }
public string _2 { get; set; }
public string _3 { get; set; }
}
public class Syncs {
public string Otherpubber { get; set; }
}
public class Root {
public string entertain { get; set; }
public string master { get; set; }
public string memail { get; set; }
public string key { get; set; }
public Mood mood { get; set; }
public string soundnumber { get; set; }
public string ftv { get; set; }
public string com { get; set; }
public Sims sims { get; set; }
public Keysecond keysecond { get; set; }
public Syncs syncs { get; set; }
}
使用这个再试一次
首先读取你的字符串 这是使用 post、get 或 delete 的响应
var response = await client.PostAsync("your-url",datasBody);
var contentData = await response.Content.ReadAsStringAsync();
var options = new JsonSerializerOptions() { PropertyNameCaseInsensitive = true };
var CommentObj = JsonSerializer.Deserialize<Root>(contentData,options);
如果您的模型不好或不匹配使用
[JsonProperty("entertain")]
public string entertain { get; set; }
,
您将需要使用 custom converter 或将您的元组转换为带有字段的单独类,以解释每个字段的用途。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。