在c#中将json对象从一种格式转换为另一种格式
本文关键字:格式 一种 转换 另一种 中将 json 对象 | 更新日期: 2023-09-27 18:17:09
我有以下格式的5种产品和12个月的价格表
dynamic product = new JObject();
product.ProductName = "Elbow Grease";
product.Price = 4.90;
product.year = 2016;
product.month = 01;
product.ProductName = "Jeans";
product.Price = 4.10;
product.year = 2016;
product.month = 01;
product.ProductName = "Jeans";
product.Price = 2.90;
product.year = 2016;
product.month = 02;
,现在我需要将这个json转换为另一种格式,如下所示发送到我的视图
[
{
"year": 2016,
"month": 01,
"Elbow Grease": 4.90,
"Jeans": 4.10
},
{
"year": "2016",
"month":"02",
"Elbow Grease": 0, //since not available
"Jeans": 2.90
}]
MVC。
你的控制器有这样的方法:
public JsonResult Products()
{
// ... your code
return Json(new { products = product }, JsonRequestBehavior.AllowGet);
}
对于webservice,返回JSON值给AJAX调用
返回方法稍作修改。
dynamic products = new DynamicJsonObject(new Dictionary<string, object>());
// ... your code to fill up dictionary
return Json.Encode(products);
注。而不是JObject,我将使用ExpandoObject
最大功率。但是,如果你仍然想使用JObject检查这个帖子Stackoverflow