C# MVC 无法反序列化元组
C# MVC can't Deserialize a tuple
我有一个 Json 模型,看起来像这样
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 对象。
您的模型有错误,请使用此站点在 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; }
让我们看看实际格式化后的数据结构
{
"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"
}
不过,这就交给你了。
您将需要使用 custom converter 或将您的元组转换为单独的 类 字段以解释每个字段的用途。
我有一个 Json 模型,看起来像这样
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 对象。
您的模型有错误,请使用此站点在 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; }
让我们看看实际格式化后的数据结构
{
"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"
}
不过,这就交给你了。
您将需要使用 custom converter 或将您的元组转换为单独的 类 字段以解释每个字段的用途。