简体   繁体   English

如何将此JSON字符串反序列化回列表 <T> ?

[英]How do I deserialize this JSON string back to a List<T>?

I am wanting to be able to use JSON to serialize and deserialize a List<T> , where the type is an interface . 我希望能够使用JSON serializedeserialize serialize List<T> ,其中类型是interface

I have the code working to serialize the List<T> to a string, but am not sure on how to deserialize the JSON string back into a List<T> . 我的代码可以将List<T> serialize字符串,但是不确定如何将JSON字符串deserialize化为List<T>

Here is my code to serialize the List<T> : 这是我的serialize List<T>代码:

List<IAndroidAsset1_1> androidAssets = new List<IAndroidAsset1_1>();

AndroidMapMarkerIconAsset1_1 androidMapMarkerIconAsset1_1 = new AndroidMapMarkerIconAsset1_1();
androidMapMarkerIconAsset1_1.Id = 1;
androidMapMarkerIconAsset1_1.icon = "TestIcon";
androidAssets.Add(androidMapMarkerIconAsset1_1);

AndroidMapMarkerSimpleImageAsset1_1 androidMapMarkerSimpleImageAsset1_1 = new AndroidMapMarkerSimpleImageAsset1_1();
androidMapMarkerSimpleImageAsset1_1.Id = 2;
androidMapMarkerSimpleImageAsset1_1.expiryDate = DateTime.UtcNow;
androidMapMarkerSimpleImageAsset1_1.webAddress = "http://www.test.com";
androidAssets.Add(androidMapMarkerSimpleImageAsset1_1);

string serializedJson = JsonConvert.SerializeObject(androidAssets, Formatting.Indented, new JsonSerializerSettings
{
    TypeNameHandling = TypeNameHandling.Objects,
    TypeNameAssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple
});

Here is the code that I have to deserialize the JSON string: 这是我必须deserializeJSON字符串的代码:

var deserializedObject = JsonConvert.DeserializeObject<IAndroidAsset1_1>(serializedJson, new JsonSerializerSettings
{
    TypeNameHandling = TypeNameHandling.Objects
});

Here is the error that I am getting: 这是我得到的错误:

An exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll but was not handled in user code Newtonsoft.Json.dll中发生类型为'Newtonsoft.Json.JsonSerializationException'的异常,但未在用户代码中处理

Additional information: Cannot deserialize the current JSON array (eg [1,2,3]) into type 'CanFindLocation.Interfaces.Android._1_1.IAndroidAsset1_1' because the type requires a JSON object (eg {"name":"value"}) to deserialize correctly. 附加信息:无法将当前JSON数组(例如[1,2,3])反序列化为类型'CanFindLocation.Interfaces.Android._1_1.IAndroidAsset1_1',因为该类型需要JSON对象(例如{“ name”:“ value”} )正确反序列化。

To fix this error either change the JSON to a JSON object (eg {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (eg ICollection, IList) like List that can be deserialized from a JSON array. 要解决此错误,可以将JSON更改为JSON对象(例如{“ name”:“ value”}),也可以将反序列化类型更改为数组,或者将实现集合接口的类型(例如ICollection,IList)更改为List,例如List从JSON数组反序列化。 JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. 还可以将JsonArrayAttribute添加到类型中,以强制其从JSON数组反序列化。

Path '', line 1, position 1. 路径'',第1行,位置1。

Can I please have some help to deserialize the JSON string back to a List<T> 我可以帮忙将JSON字符串deserialize List<T>吗?

Thanks in advance. 提前致谢。

You are serailizing a list not a single IAndroidAsset1_1 object, so you need deserialize to List<IAndroidAsset1_1> type not IAndroidAsset1_1 : 您要对列表进行序列化,而不是对单个IAndroidAsset1_1对象进行序列化,因此需要反序列化为List<IAndroidAsset1_1>类型而不是IAndroidAsset1_1

var deserializedObject = JsonConvert.DeserializeObject<List<IAndroidAsset1_1>>(serializedJson, new JsonSerializerSettings
{
     TypeNameHandling = TypeNameHandling.Objects
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM