2012-04-06 254 views
2

我使用Rob Conery的Massive框架從數據庫中選擇IEnumerable<dynamic>。該結構以Poco C#平面格式返回。我需要轉換數據並將其輸出到Json數組(格式顯示在底部)。將IEnumerable <dynamic>轉換爲JsonArray

我想我可以做使用LINQ變換(我的成功努力,如下圖所示):

using System.Collections.Generic; 
using System.Json; 
using System.Linq; 
using System.ServiceModel.Web; 

.... 
    IEnumerable<dynamic> list = _repository.All("", "", 0).ToList(); 

    JsonArray returnValue = from item in list 
          select new JsonObject() 
             { 
               Name = item.Test, 
               Data = new dyamic(){...}... 
             }; 

這裏是JSON的我試圖生成:

[ 
    { 
     "id": "1", 
     "title": "Data Title", 
     "data": [ 
      { 
       "column1 name": "the value", 
       "column2 name": "the value", 
       "column3 name": "", 
       "column4 name": "the value" 
      } 
     ] 
    }, 
    { 
     "id": "2", 
     "title": "Data Title", 
     "data": [ 
      { 
       "column1 name": "the value", 
       "column2 name": "the value", 
       "column3 name": "the value", 
       "column4 name": "the value" 
      } 
     ] 
    } 
] 
+1

首先,你爲什麼在這種情況下使用'dynamic'?據推測'_repository'是鍵入的?其次,爲什麼不使用像JavaScriptSerializer或JSON.net這樣的序列化程序?第三,'JsonValue'是抽象的,所以你不能實例化它。 – 2012-04-06 16:52:05

+1

您是否調查了DataContractJsonSerializer http://msdn.microsoft.com/en-us/library/bb412179.aspx? – Phil 2012-04-06 16:53:29

+1

你有沒有看過JayRock for .Net?看起來不錯。我認爲你可以將對象解析爲JsonObjects。 – Greg 2012-04-06 16:53:32

回答

3

下面是一個例子使用Json.Net

List<int> list = new List<int>() {1 , 2}; 

string json = JsonConvert.SerializeObject(
        list.Select(x => new{ 
         id = x.ToString(), 
         title = "title " + x.ToString(), 
         data = Enumerable.Range(3,2).Select(i=> new {column1=i,column2=i*i}) 
        }) 
        , Newtonsoft.Json.Formatting.Indented 
       ); 

輸出:

[ 
    { 
    "id": "1", 
    "title": "title 1", 
    "data": [ 
     { 
     "column1": 3, 
     "column2": 9 
     }, 
     { 
     "column1": 4, 
     "column2": 16 
     } 
    ] 
    }, 
    { 
    "id": "2", 
    "title": "title 2", 
    "data": [ 
     { 
     "column1": 3, 
     "column2": 9 
     }, 
     { 
     "column1": 4, 
     "column2": 16 
     } 
    ] 
    } 
] 
+0

謝謝,有點擔心我的問題沒有使有任何意義:) – Burt 2012-04-06 17:30:25

0

確定這裏是我結束了這一切看起來虎背熊腰,腳蹬多莉:

[WebGet(UriTemplate = "/tools/data/get?tool={tool}&filters={filters}")] 
    public JsonArray GetData(string tool, string[,] filters) 
    { 
     IEnumerable<dynamic> list = _repository.All("", "", 0).ToList(); 


     IEnumerable<JsonObject> jsonList = from item in list 
           select new JsonObject() 
              { 
               new KeyValuePair<string, JsonValue>("Id", item.Id), 
               new KeyValuePair<string, JsonValue>("Name", item.Title), 
               new KeyValuePair<string, JsonValue>("Data", new JsonObject() 
                           { 
                            new KeyValuePair<string, JsonValue>("Product", item.Product), 
                            new KeyValuePair<string, JsonValue>("Suite", item.Suite), 
                            new KeyValuePair<string, JsonValue>("Package", item.Package), 
                            new KeyValuePair<string, JsonValue>("Description", item.Description) 
                           }) 

              }; 

     JsonArray returnValue = new JsonArray(jsonList); 

     return returnValue; 
    } 
相關問題