[英]Json deserialize string with 2 arrays
I have to Serialize a string with two list collections as JSON string. 我必须将具有两个列表集合的字符串序列化为JSON字符串。
public class Product {
public string Name {get;set;}
};
public class Product2 {
public string Age { get; set; }
};
private void Form1_Load(object sender, EventArgs e) {
List<Product> mitems = new List<Product>();
List<Product2> mitems2 = new List<Product2>();
mitems.Add(new Product { Name = "John" });
mitems2.Add(new Product2 {Age = "28" } );
var objects = new { mitems, mitems2 };
string json = JsonConvert.SerializeObject(objects);
var Items = JsonConvert.DeserializeObject<List<Product>>(json);
}
In the last line, I am getting an exception Cannot deserialize the current JSON object
在最后一行,我得到一个异常
Cannot deserialize the current JSON object
Instead of an anonymous object probably you would want to create a model to hold this like 您可能想创建一个模型来持有这样的模型,而不是匿名对象
public class MyModel
{
public List<Product> products { get; set; }
public List<Product2> products2 { get; set; }
}
In Form_Load
在
Form_Load
MyModel model = new MyModel
{
Products = mitems,
Products2 = mitems2,
}
var Items = JsonConvert.DeserializeObject<MyModel>(json);
per your comment, if you want to iterate through Products then 根据您的评论,如果您要遍历产品,则
foreach(var data in Item.products)
{
MessageBox.Show(data.Name);
}
You can't deserialise objects to a List<Product>
because it isn't a List<Product>
it's a new anonymous type with two properties 您不能将对象反序列化为
List<Product>
因为它不是List<Product>
它是具有两个属性的新匿名类型
Change 更改
var objects = new { mitems, mitems2 };
To 至
var objects = new List<Product>(mitems);
objects.AddRange(mitems2);
Also Product2 is not a product 另外Product2不是产品
You also need to change 您还需要更改
public class Product2
To 至
public class Product2 : Product
in order to add it to a list of Product 为了将其添加到产品列表中
You have several options, one is you can deserialize as dynamic
or you can define a root object for your final json. 您有几种选择,一种是可以将其反序列化为
dynamic
,也可以为最终的json定义一个根对象。
var Items = JsonConvert.DeserializeObject<dynamic>(json);
and casting to your object lists: 并投射到您的对象列表:
List<Product> products = new List<Product>();
List<Product2> products2 = new List<Product2>();
foreach (var item in Items.mitems)
{
Console.WriteLine(item.Name);
products.Add(((JObject)item).ToObject<Product>());
}
foreach (var item2 in Items.mitems2)
{
Console.WriteLine(item2.Age);
products2.Add(((JObject)item2).ToObject<Product2>());
}
output : 输出:
John
28
or 要么
class RootObj
{
public List<Product> mitems { get; set; }
public List<Product2> mitems2 { get; set; }
}
var Items = JsonConvert.DeserializeObject<RootObj>(json);
foreach (var item in Items.mitems)
{
Console.WriteLine(item.Name);
}
foreach (var item2 in Items.mitems2)
{
Console.WriteLine(item2.Age);
}
output: 输出:
John
28
Outputs for both deserialization methods: 两种反序列化方法的输出:
you can also do this: 您也可以这样做:
using Newtonsoft.Json.Linq;
var item = JsonConvert.DeserializeObject<IDictionary<string, object>>(json)["mitems"];
Console.WriteLine(((JArray)item).ToObject<IList<Product>>());
Edit: I rephrased my answer 编辑:我改了我的答案
Fiddler Example : https://dotnetfiddle.net/ItMClb Fiddler示例: https : //dotnetfiddle.net/ItMClb
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.