【问题标题】:MDX result to JSON using C#使用 C# 将 MDX 结果转为 JSON
【发布时间】:2013-03-28 16:14:20
【问题描述】:

我是 MDX 新手,基本上我需要将 SSAS MDX 结果序列化为 JSON 对象。

MDX 查询:

SELECT
(
    [Measures].[Max Available]
) ON COLUMNS
, NON EMPTY 
(
    [Application].[Product].Children * [Application].[Application Name].Children
) DIMENSION PROPERTIES MEMBER_CAPTION ON ROWS
FROM [Applications]

假设我的 MDX 结果如下所示:

__________________________________
|          |          | Measure1 |
| Product1 | Feature1 | 1        |
| Product1 | Feature2 | 1        |
| Product1 | Feature3 | 10       |
| Product2 | Feature1 | 1        |
| Product2 | Feature2 | 1        |
| Product3 | Feature1 | 1        |
| Product3 | Feature2 | 1        |
| Product3 | Feature3 | 1        |
| Product3 | Feature4 | 1        |

我需要创建一个看起来像这样的 JSON 对象(我不需要测量值,我只是使用它们来获取 MDX 层次结构中的有效产品和功能列表):

[
 {
   "product":"Product1",
   "feature":[
      "Feature1",
      "Feature2",
      "Feature3"
   ]
 }, {
   "product":"Product2",
   "feature":[
      "Feature1",
      "Feature2"
   ]
 }, {
   "product":"Product3",
   "feature":[
      "Feature1",
      "Feature2",
      "Feature3",
      "Feature4"
   ]
 }, {
   ...
 }
]

我通过 ExecuteCellSet() 使用 ADOMD.NET 库,但我也是这个库的新手。有人能指出我正确的方向吗?

谢谢, 狐狸

【问题讨论】:

  • 我也坚持最好的方法。任何帮助将不胜感激。

标签: json c#-4.0 mdx


【解决方案1】:

虽然我不熟悉 MDX,但我在 C# 中使用过 JSON。创建 JSON 对象的一种方法(因为它们在 C# 4.5 中不是本机的)是使用 DataContracts。

using System.Runtime.Serialization.Json;
using System.Runtime.Serialization;

namespace Interface.Data
{
    [DataContract]
    class jsonObject
    {        
        [DataMember]
        public String product="";
        [DataMember]
        public String[] feature={"", "", ""};

        public jsonObject(){}
    }
}

然后在您的 MDX 查询中,假设您将查询数据存储在某个字符串“行”中,您可以这样做:

using System.Runtime.Serialization.Json;
using System.Data.Common;
using System.IO;

static jsonObject json;
public static MDXObj parseMDXObj(String line)
{   
    MDXObj obj = new MDXObj(getMDX());
    try
    {     
        DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(jsonObject));
        MemoryStream stream = new MemoryStream(ASCIIEncoding.UTF8.GetBytes(line));
        json = (jsonObject)ser.ReadObject(stream);

    }
    catch (Exception e)
    {
        Console.WriteLine("Error Occurred in creating JSON: " + e.Message);
        return null;
    }
    return obj;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-25
    • 2011-06-26
    • 1970-01-01
    • 2016-08-02
    • 2017-10-15
    • 2021-10-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多