C# 如何在数组中拆分键/值对?
我的数据库中存储了这种类型的键值:C# 如何在数组中拆分键/值对?,c#,json,C#,Json,我的数据库中存储了这种类型的键值: [{"SelFoodId":"2","SelQuantity":"5"}, {"SelFoodId":"7","SelQuantity":"3"}, {"SelFoodId":"9","SelQuantity":"7"}] 但现在我想用c#like将这个JSON数组拆分成这种形式 1。转到此处()并用JSON创建一些对象 这会给你类似的东西 public class MyAwesomeSomething { public string SelFo
[{"SelFoodId":"2","SelQuantity":"5"},
{"SelFoodId":"7","SelQuantity":"3"},
{"SelFoodId":"9","SelQuantity":"7"}]
但现在我想用c#like将这个JSON数组拆分成这种形式
1。转到此处()并用JSON创建一些对象 这会给你类似的东西
public class MyAwesomeSomething
{
public string SelFoodId { get; set; }
public string SelQuantity { get; set; }
}
2.添加Nuget包
3.向上看,然后
将JSON反序列化为.NET对象.n
4.编写一些代码
Exmaple
var json = "[{\"SelFoodId\":\"2\",\"SelQuantity\":\"5\"},{\"SelFoodId\":\"7\",\"SelQuantity\":\"3\"},{\"SelFoodId\":\"9\",\"SelQuantity\":\"7\"}]";
var list = JsonConvert.DeserializeObject<List<MyAwesomeSomething>>(json);
foreach(var item in list)
{
Console.WriteLine(item.SelFoodId + " " + item.SelQuantity);
}
您尝试过的是什么。反序列化JSON。2.在某种循环中创建两个列表。3.4.恐怕你的问题目前还不清楚,部分原因是你的输入中有6个值,预期输出中有4个值。我强烈建议使用Json.NET解析到一个
列表
,其中Foo
有SelFoodId
和SelQuantity
属性,然后从那里开始工作。@john,+1为了利润,这让我很开心
var json = "[{\"SelFoodId\":\"2\",\"SelQuantity\":\"5\"},{\"SelFoodId\":\"7\",\"SelQuantity\":\"3\"},{\"SelFoodId\":\"9\",\"SelQuantity\":\"7\"}]";
var list = JsonConvert.DeserializeObject<List<MyAwesomeSomething>>(json);
foreach(var item in list)
{
Console.WriteLine(item.SelFoodId + " " + item.SelQuantity);
}
2 5
7 3
9 7