2016-11-08 45 views
0

我有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 
    }] 
+0

你確定你的json例子有效嗎? –

回答

0

在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

相關問題