【问题标题】:Extract KeyValue pair and convert it into JSON (Column and rows) using jquery提取键值对并使用 jquery 将其转换为 JSON(列和行)
【发布时间】:2014-06-18 03:31:15
【问题描述】:

我正在研究 Asp.net MVC。我想将 Excel 数据导入我的一个网格。我使用ExpandoObject(动态)读取Excel文件,然后将其转换为键/值对。我想知道如何提取 KeyValue 对并从该键/值对中以 JSON 格式生成标题和数据行。

数据将如下所示:

我想将此 KeyValue 转换为 Header 和行,如:

[
    {"CustomerName" : "Messi", "City":"Buenos Aires", "Country" :"Argentina"},
    {"CustomerName" : "Ronaldo", "City":"Lisbon", "Country" :"Portugal"},
    {"CustomerName" : "Persie", "City":"Amsterdam", "Country" :"Netherlands"},
    {"CustomerName" : "Ronnie", "City":"London", "Country" :"England"}
]

我用过下面的 jQuery sn-p 但它不起作用:

var arr = [];

$.each(dataReceivedFromServer, function (i, data) {
  var d = data.Key;
  arr.push({
         d: data.Value,
         });
  });

如果我执行上述操作,它会显示[{"d":"Messi"},{"d":"Buenos Aires"},{"d":"Argentina"} 等等...

简而言之,我想将 Excel 文件中的数据绑定到 KendoUI 网格(所有列都是动态的,因为用户可以选择任何文件)。

有人知道怎么做吗?

更新 #1:

@webkit - 请看下图作为 Console.log 的输出

【问题讨论】:

  • 这在很大程度上取决于数据的样子
  • 我无法修复列名,因为数据始终是动态的。我对此没有任何控制权。
  • 每个循环中的“数据”是什么?为了调试这个,我会控制台 --> for (var i in data) console.log(i," :: ", data[i]); --- 看看你得到了什么。如果可以的话,把它贴在这里。
  • 我已经用 Console.log 的结果更新了问题
  • @cr0ss - 谢谢。但是,实际上我想创建 JSON。但是在这个 jsfiddle 中,你定义了我不想要的静态 JSON。

标签: c# jquery asp.net-mvc json excel


【解决方案1】:

我强烈建议您在收到数据之前对其进行转换,因为这样会更可靠。但是,如果这是不可能的,并且您可以保证您的数据集上有一些分组规则,您可以应用以下内容:

var customer = {
    //this transform asserts the key value collection will be in a groupable order
    transform: function(coll, groupBeginKey){
        var result = [];
        var currentObject = {};
        var size = coll.length;
        $.each(coll,function(idx,data){
            //check if we are beginning 
           var defined = (typeof(currentObject[data.Key]) !== 'undefined');

            if(!defined && 
               data.Key === groupBeginKey){
                currentObject = {};
            }else if((defined &&
               data.Key === groupBeginKey)){

                result.push(currentObject);
                currentObject = {};
            }
            //set the property on the object
            currentObject[data.Key] = data.Value;
            //handle last
            if(idx >= size-1){
                result.push(currentObject);
            }
        });

        return result;
    }
};

对于以下数据集(为简单起见,我对其进行了简化):

var data = []

data.push({ "Key": "CustomerName", "Value": "Paul"});
data.push({ "Key": "City", "Value": "Las Vegas"});
data.push({ "Key": "CustomerName", "Value": "George"});
data.push({ "Key": "Zip", "Value": "1234567"});
data.push({ "Key": "City", "Value": "New York"});
data.push({ "Key": "CustomerName", "Value": "Joe"});
data.push({ "Key": "City", "Value": "Omaha"});

断言客户可以从 CustomerName 分组到 City,您可以按如下方式使用它(它将包括具有邮政编码的客户):

var results = customer.transform(data,"CustomerName");

请在下面的 jsfiddle 中查看它的运行情况(在调试控制台中查看输出):

http://jsfiddle.net/pHQze/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-20
    • 2021-10-02
    • 2020-08-30
    • 1970-01-01
    • 2021-03-10
    • 1970-01-01
    相关资源
    最近更新 更多