2

我正在IEnumerable<dynamic>使用 Rob Conery 的 Massive 框架从数据库中选择一个。该结构以平面格式 Poco C# 返回。我需要转换数据并将其输出到 Json 数组(格式显示在底部)。

我以为我可以使用 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"
            }
        ]
    }
]
4

2 回答 2

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
      }
    ]
  }
]
于 2012-04-06T17:26:04.003 回答
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;
    }
于 2012-04-06T17:43:03.360 回答