2012-04-06 217 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; 
    } 
相关问题