转换JSON从一种格式对象到另一个在C#
问题描述:
我有5个产品,价格为12个月的列出以下格式转换JSON从一种格式对象到另一个在C#
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);
}
在Web服务的情况下,返回的JSON值AJAX调用
返回方法稍作修改。
dynamic products = new DynamicJsonObject(new Dictionary<string, object>());
// ... your code to fill up dictionary
return Json.Encode(products);
p.s.我将使用ExpandoObject代替JObject
p.p.s.但如果你仍然想使用JObject检查这个职位Stackoverflow
+0
问题在于属性'ProductName'的值现在是属性,其值是'price'属性的值。 另外,所有的ProductName都将被组合在同一个对象中,其corr值为同一年/月。 – Aarush
你确定你的json例子有效吗? –