0

我的SQL Server数据库上有 2 个表,然后我想合并到我的 WCF 服务上的 1 个 JSON 数组中。

表格1:

----------------
| type | total |
----------------
|   A  |   2   |
|   B  |   3   |
|   C  |   4   |
----------------

表2:

----------------
| type | total |
----------------
|   A  |   5   |
|   B  |   6   |
|   C  |   7   |
----------------

我想要这样的 JSON 数组:

{"GetResult":
        [
          {"table1":
               [
                 {"type":"A", "total":"2"},
                 {"type":"B", "total":"3"},
                 {"type":"C", "total":"4"}
               ]
          },
          {"table2":
               [
                 {"type":"A", "total":"5"},
                 {"type":"B", "total":"6"},
                 {"type":"C", "total":"7"}
               ]
          }
        ]
}
4

2 回答 2

1

首先,您需要将数据表转换为JSONArray. 此链接将为您提供帮助。

然后你需要组合 json 数组的 using ,如组合 JSON 数组JavaScript中所做的那样

希望能帮助到你

于 2012-06-26T08:43:44.237 回答
1

您可以创建您的数据合同,例如

[DataContract]
public class ResultData
{
    [DataMember]
    public List<DataItem> Table1 { get; set; }

    [DataMember]
    public List<DataItem> Table2 { get; set; }
}

[DataContract]
public class DataItem
{
    [DataMember]
    public string Type { get; set; }

    [DataMember]
    public string Total{ get; set; }

}

然后将服务合同添加为

 [WebGet(UriTemplate = "/ResultData", ResponseFormat = WebMessageFormat.Json)]
 ResultData GetJsonData();
于 2012-06-26T08:58:37.760 回答