我需要转换它 -
{"Header":{ "Number" : 101, "Total" : 100.00},
"Items" : [
{"Line" : 1, "Description": "Item 1", "Price" : 25.00, "Quantity" : 2},
{"Line" : 2, "Description": "Item 2", "Price" : 50.00, "Quantity" : 1}
]}
到此 -
[
{"HeaderNumber" : 101, "Total" : 100.00, "Line" : 1, "Description": "Item 1", "Price" : 25.00, "Quantity" : 2},
{"HeaderNumber" : 101, "Total" : 100.00, "Line" : 2, "Description": "Item 2", "Price" : 50.00, "Quantity" : 1}
]
enter code here
关于如何实现这一目标的任何高层次想法? 提前谢谢。
答案 0 :(得分:1)
为您可以使用JSONToCSharp创建一个JSON Model类。一旦你得到像这样的JSON结果存储
JsonModel[] result = JsonConvert.DeserializeObject<JsonModel[]>(postResult);
它会将您的结果转换为Model类的Array。然后你可以使用foreach循环迭代它。
foreach (var item in result)
{
item.PropertyName;
}
答案 1 :(得分:1)
试试这个:)
public class Header
{
public int Number { get; set; }
public double Total { get; set; }
}
public class Item
{
public int Line { get; set; }
public string Description { get; set; }
public double Price { get; set; }
public int Quantity { get; set; }
}
public class Source
{
public Header Header { get; set; }
public List<Item> Items { get; set; }
}
public class Target
{
public int HeaderNumber { get; set; }
public double Total { get; set; }
public int Line { get; set; }
public string Description { get; set; }
public double Price { get; set; }
public int Quantity { get; set; }
}
class Program
{
static void Main(string[] args)
{
var src = @"{""Header"":{ ""Number"" : 101, ""Total"" : 100.00},
""Items"" : [
{""Line"" : 1, ""Description"": ""Item 1"", ""Price"" : 25.00, ""Quantity"" : 2},
{""Line"" : 2, ""Description"": ""Item 2"", ""Price"" : 50.00, ""Quantity"" : 1}
]}";
var srcObj = JsonConvert.DeserializeObject<Source>(src);
var targetObj = srcObj.Items.Select(s => new Target()
{
HeaderNumber = srcObj.Header.Number,
Total = srcObj.Header.Total,
Description = s.Description,
Line = s.Line,
Price = s.Price,
Quantity = s.Quantity
});
Console.WriteLine(JsonConvert.SerializeObject(targetObj));
Console.ReadLine();
}
}