jQuery 将数组转换为 JSON

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19262738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 23:32:52  来源:igfitidea点击:

Converting an array into JSON

javascriptjqueryarraysjsoncsv

提问by Adrian

I need to bring in a csv doc and convert it to JSON, so far I have been able to convert it to an array and from the array I'm trying to build a JSON object.

我需要引入一个 csv 文档并将其转换为 JSON,到目前为止我已经能够将它转换为一个数组,并且我正在尝试从该数组中构建一个 JSON 对象。

Below is the JavaScript that builds the JSON, but its not in the structure I need, underneath is an example of the structure required.

下面是构建 JSON 的 JavaScript,但它不在我需要的结构中,下面是所需结构的示例。

var jsonObj = []; //declare object

for (var i=1;i<csvAsArray.length;i++) { 
  jsonObj.push({key: csvAsArray[i][0]}); //key

    for (var l=1;l<csvAsArray[0].length;l++) { 
      jsonObj.push({label: csvAsArray[0][l], values: csvAsArray[i][l]}); //label + value respectively
    }
}

Final output required:

所需的最终输出:

{
  "key": "Sample 01",
  "values": [
    { 
      "label" : "Something" ,
      "value" : 1
    } , 
    { 
      "label" : "Something" ,
      "value" : 2
    }
  ]
},
{
  "key": "Sample 02",
  "values": [
    { 
      "label" : "Something" ,
      "value" : 5
    } , 
    { 
      "label" : "Something" ,
      "value" : 4
    }
  ]
}

采纳答案by Wing Lian

You need to declare the values and push that onto a tmp variable before pushing that index onto the final/main object

在将该索引推送到最终/主对象之前,您需要声明值并将其推送到 tmp 变量

var tmp_values, jsonObj = []; //declare object

for (var i=1;i<csvAsArray.length;i++) {
    var tmp_values = [];
    for (var l=1;l<csvAsArray[0].length;l++) { 
        tmp_values.push({label: csvAsArray[0][l], value: csvAsArray[i][l]}); //label + value respectively
    }
    jsonObj.push({key: csvAsArray[i][0], values: tmp_values}); //key
}

回答by iJade

Simply use JSON.stringify()to convert your array to json string

只需用于JSON.stringify()将您的数组转换为 json 字符串

var jsonString = JSON.stringify(yourArray);

回答by Vidya

This scriptis a common way to convert an array to JSON in JavaScript.

脚本是 JavaScript 中将数组转换为 JSON 的常用方法。

回答by Marty

I'm not a fan of answers like this, but this servicein my opinion is good enough to warrant it. It can convert between various data formats including CSV, JSON, XML, HTML table, etc. It even offers variations in output structure for some of the formats.

我不喜欢这样的答案,但在我看来,这项服务足够好,值得。它可以在各种数据格式之间进行转换,包括 CSV、JSON、XML、HTML 表等。它甚至为某些格式提供输出结构的变化。

回答by Behnam Mohammadi

use this code and very simple develop for more two array

使用此代码并非常简单地开发更多两个数组

function getJSON(arrayID,arrayText) {    
    var JSON = "[";
    //should arrayID length equal arrayText lenght and both against null
    if (arrayID != null && arrayText != null && arrayID.length == arrayText.length) {
        for (var i = 0; i < arrayID.length; i++) {
            JSON += "{";
            JSON += "text:'" + arrayText[i] + "',";
            JSON += "id:'" + arrayID[i] + "'";
            JSON += "},";
        }
    }
    JSON += "]"
    JSON = Function("return " + JSON + " ;");
    return JSON();
}

and 3 array

和 3 个数组

function getJSON(arrayID, arrayText, arrayNumber) {
    var JSON = "[";  
    if (arrayID != null && arrayText != null && arrayNumber!=null && Math.min(arrayNumber.length,arrayID.length)==arrayText.length) {
        for (var i = 0; i < arrayID.length; i++) {
            JSON += "{";
            JSON += "text:'" + arrayText[i] + "',";
            JSON += "id:'" + arrayID[i] + "',";
            JSON += "number:'" + arrayNumber[i] + "'";
            JSON += "},";
        }
    }
    JSON += "]"
    JSON = Function("return " + JSON + " ;");
    return JSON();
}