C# 由于字段名,无法反序列化当前JSON数组
我有json(简化版)C# 由于字段名,无法反序列化当前JSON数组,c#,json,.net,json.net,.net-4.5,C#,Json,.net,Json.net,.net 4.5,我有json(简化版) 公共类json存储 { [JsonProperty(“$type”)]公共字符串类型{get;set;} [JsonProperty(“$values”)]公共列表值{get;set;} } 公共类JsonTest { [JsonProperty(“Name”)]公共字符串名称{get;set;} [JsonProperty(“Id”)]公共字符串Id{get;set;} } 反序列化时 JsonConvert.DeserializeObject<JsonStor
公共类json存储
{
[JsonProperty(“$type”)]公共字符串类型{get;set;}
[JsonProperty(“$values”)]公共列表值{get;set;}
}
公共类JsonTest
{
[JsonProperty(“Name”)]公共字符串名称{get;set;}
[JsonProperty(“Id”)]公共字符串Id{get;set;}
}
反序列化时
JsonConvert.DeserializeObject<JsonStorage>(json);
JsonConvert.DeserializeObject(json);
抛出异常
Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Program+JsonStorage' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
Path '$values', line 1, position 34.
Newtonsoft.Json.JsonSerializationException:无法将当前Json数组(例如[1,2,3])反序列化为类型“Program+JsonStorage”,因为该类型需要一个Json对象(例如{“name”:“value”})才能正确反序列化。
要修复此错误,请将JSON更改为JSON对象(例如{“name”:“value”}),或将反序列化类型更改为数组或实现可从JSON数组反序列化的集合接口(例如ICollection、IList)类似列表的类型。还可以将JsonArrayAttribute添加到类型中,以强制它从JSON数组反序列化。
路径“$values”,第1行,位置34。
如果在json字符串$type
->type
和$values
->values
中替换并为该json设置json属性,反序列化对象
工作正常。我可以用这种方式,在反序列化之前替换。但也许有更好的选择
示例:使用粘贴json时为我创建的类反序列化发布的数据时,我没有遇到任何问题(尽管它不喜欢后面的逗号)
namespace WindowsFormsApp3cs
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
私有void SomeButton_单击(对象发送者,事件参数e)
{
var json=@“{”“$type”“:”“某些类型”“,$values”“:[{”“类型”“:”“某些类型”“,”“名称”“:”“,”“Id”“:”“某些值”“,}]}”;
var rootClassNameHere=rootClassNameHere.FromJson(json);
}
}
公共部分类RootClassNameHere
{
[JsonProperty(“$type”)]
公共字符串类型{get;set;}
[JsonProperty(“$values”)]
公共值[]值{get;set;}
}
公共部分类值
{
[JsonProperty(“类型”)]
公共字符串类型{get;set;}
[JsonProperty(“名称”)]
公共字符串名称{get;set;}
[JsonProperty(“Id”)]
公共字符串Id{get;set;}
}
公共部分类RootClassNameHere
{
公共静态RootClassNameHere FromJson(字符串json)=>JsonConvert.DeserializeObject(json,WindowsFormsApp3cs.Converter.Settings);
}
公共静态类序列化
{
公共静态字符串ToJson(此RootClassNameHere self)=>JsonConvert.SerializeObject(self,WindowsFormsApp3cs.Converter.Settings);
}
内部静态类转换器
{
公共静态只读JsonSerializerSettings设置=新JsonSerializerSettings
{
MetadataPropertyHandling=MetadataPropertyHandling.Ignore,
DateParseHandling=DateParseHandling.None,
转换器=
{
新的IsoDateTimeConverter{DateTimeStyles=DateTimeStyles.AssumeUniversal}
},
};
}
}
看看,我猜你的绳子被顶起来了@“{”“$type”“:-@符号表示按原样使用我的字符串。然后您有双引号。因此JSON转换器可能会呕吐,因为它不知道该做什么。请尝试像我说的那样删除双引号,只是根据您发布的@“\u”“\ u=”\ u\”进行猜测。”
@AnthonyRussell我想Genusatplay是想说,加倍“
是一种在@string
中转义的方法。。
JsonConvert.DeserializeObject<JsonStorage>(json);
Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Program+JsonStorage' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
Path '$values', line 1, position 34.
namespace WindowsFormsApp3cs
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void SomeButton_Click(object sender, EventArgs e)
{
var json = @"{""$type"": ""some type"",""$values"": [{""type"": ""some type"",""Name"": """",""Id"": ""someValue"",}]}";
var rootClassNameHere = RootClassNameHere.FromJson(json);
}
}
public partial class RootClassNameHere
{
[JsonProperty("$type")]
public string Type { get; set; }
[JsonProperty("$values")]
public Value[] Values { get; set; }
}
public partial class Value
{
[JsonProperty("type")]
public string Type { get; set; }
[JsonProperty("Name")]
public string Name { get; set; }
[JsonProperty("Id")]
public string Id { get; set; }
}
public partial class RootClassNameHere
{
public static RootClassNameHere FromJson(string json) => JsonConvert.DeserializeObject<RootClassNameHere>(json, WindowsFormsApp3cs.Converter.Settings);
}
public static class Serialize
{
public static string ToJson(this RootClassNameHere self) => JsonConvert.SerializeObject(self, WindowsFormsApp3cs.Converter.Settings);
}
internal static class Converter
{
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
{
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
DateParseHandling = DateParseHandling.None,
Converters =
{
new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
},
};
}
}