我以以下格式从[HttpPost]
获取JSON数据。我想反序列化为两个C#模型。
data {{
"invoiceNumber": "55",
"invoiceDate": "2018-10-25T13:16:37.843Z",
"invoiceValue": 55,
"remarks": null,'
"items": [
{
"gRNNo": "0000000",
"itemCode": 1,
"itemDesc": "Lux",
"qty": "2",
},
{
"gRNNo": "0000000",
"itemCode": 2,
"itemDesc": "Rexona",
"qty": "1"
}
]
}}
我可以获取Header数据,但可以获取项数组。
PurHeader purHeader = JsonConvert.DeserializeObject<PurHeader>(data.ToString()); //Working for Header
,但无法获取数组数据。我尝试跟随。
List<PurDetail> purDetail = JsonConvert.DeserializeObject<List<PurDetail>>(data["items"].ToString());
var purDetail = JsonConvert.DeserializeObject<PurDetail[]>(data["items"].ToString());
我的课程-我在“代码优先”中使用它们。并且data
json来自Angular 7 ReactiveForms
public class PurHeader
{
public string GRNNo { get; set; }
public string InvoiceNumber { get; set; }
public DateTime InvoiceDate { get; set; }
public decimal InvoiceValue { get; set; }
public string Remarks { get; set; }
public ICollection<PurDetail> PurDetail { get; set; }
}
public class PurDetail
{
public string GRNNo { get; set; }
public string ItemCode { get; set; }
public string ItemDesc { get; set; }
public decimal Qty { get; set; }
[ForeignKey("GRNNo")]
public PurHeader PurHeader { get; set; }
}
答案 0 :(得分:0)
您的JSON格式不正确,我希望这只是您输入的错误。但是,您应该可以使用以下类来解析整个集合:
public class Rootobject
{
public string invoiceNumber { get; set; }
public DateTime invoiceDate { get; set; }
public int invoiceValue { get; set; }
public object remarks { get; set; }
public Item[] items { get; set; }
}
public class Item
{
public string gRNNo { get; set; }
public int itemCode { get; set; }
public string itemDesc { get; set; }
public string qty { get; set; }
}
Rootobject purHeader = JsonConvert.DeserializeObject<Rootobject>(data.ToString());
另请参阅我关于如何轻松地从XML派生类的答案(同样适用于JSON):Parse This XML to object
答案 1 :(得分:0)
假设您的SELECT
id,
name,
MIN(date) AS firstdate,
MAX(date) AS maxdate
FROM View_MySource
GROUP BY
id,
name;
没有两个data
,您的代码应该可以工作。
但是您可以这样做而不必强制转换为 string ,只是:
{{