【问题标题】:Extracting arrays out of a string that isn't valid JSON从无效 JSON 的字符串中提取数组
【发布时间】:2020-12-01 16:29:52
【问题描述】:

我有一个数据对象,它是从某个预定义的数据库表的列中获取的,该列包含两个数组,如下所示:

const distribution_bins = "{[0.0, 0.3, 0.6, 0.9, 1.2, 1.5, 1.8, 2.1, 2.4, 2.7, +INF], [0.0, 0.12, 0.24, 0.36, 0.48, 0.6, 0.72, 0.84, 0.96, +INF]}";

我需要从中提取单独的数组:

 array1 = [0.0, 0.3, 0.6, 0.9, 1.2, 1.5, 1.8, 2.1, 2.4, 2.7, +INF],
 array2 = [0.0, 0.12, 0.24, 0.36, 0.48, 0.6, 0.72, 0.84, 0.96, +INF]

为此,我正在对distribution_bins 执行如下操作:

console.log(distribution_bins.split('],'));

但这样做会产生如下数据:

[
  '{[0.0, 0.3, 0.6, 0.9, 1.2, 1.5, 1.8, 2.1, 2.4, 2.7, +INF',
  ' [0.0, 0.12, 0.24, 0.36, 0.48, 0.6, 0.72, 0.84, 0.96, +INF]}'
]

注意第一个数组[0.0, 0.3,....,2.7, +INF' 后面缺少的] 右括号。​​

不确定完成的 split() 是否不正确。任何纠正此问题的帮助表示赞赏:)

【问题讨论】:

  • 您的第一个 sn-p 语法不正确。
  • 它是一个对象吗?还是字符串?
  • 配合什么是+INF?它在 javascript 中没有任何意义。
  • 字符串中的“对象”没有数组值的键。你的意思是让它成为一个数组数组吗?

标签: javascript arrays json


【解决方案1】:

您的输入不是有效的 JSON,但很接近。如果您对其有任何控制权,则应将其更改为有效的 Json。如果你不这样做,你可以这样做:

// Create keys for the properties of the objects. This uses the offset
// of the start of the property as its key.
let jsonify = distribution_bins.replace(/{/g, "[");
jsonify = jsonify.replace(/}/g, "]");

// Wrap +INF in quotes.
jsonify = jsonify.replace(/\+INF/g, `"+INF"`);

// Parse.
const distribution_data = JSON.parse(jsonify);

【讨论】:

  • 是的,我对收到的数据没有权力。而且,尝试您提供的解决方案,它会抛出错误:TypeError: JSON.decode is not a function
  • 应该是 JSON.parse。
  • 你能帮忙解释一下输出吗? { '1': [ 0, 0.3, 0.6, 0.9, 1.2, 1.5, 1.8, 2.1, 2.4, 2.7, '+INF' ], '59': [ 0, 0.12, 0.24, 0.36, 0.48, 0.6, 0.72, 0.84, 0.96, '+INF' ]这些“1”和“59”随机数代表什么?
  • 是的,或者代替第一件事,将“{”替换为“[”,将“}”替换为“]”。
  • 这比添加键要简单得多。
【解决方案2】:

您可以尝试使用字符串和数组操作这一块,我采取了额外的步骤来使代码示例清晰,但是您可以通过使用映射来优化和减少代码行来遍历双精度数组值。

let distribution_bins = "{[0.0, 0.3, 0.6, 0.9, 1.2, 1.5, 1.8, 2.1, 2.4, 2.7, +INF], [0.0, 0.12, 0.24, 0.36, 0.48, 0.6, 0.72, 0.84, 0.96, +INF]}";

// clean up the edge "[{ }]" characters 
distribution_bins = distribution_bins.substring(2, distribution_bins.length-2);

// Create double dimension array, each one with a one string value
let doubleArray = distribution_bins.split("], [");
const doubleArraySplit0 = doubleArray[0].split(",");
const doubleArraySplit1 = doubleArray[1].split(",");

// Parse each string to a number using a map
const strToNum0 = doubleArraySplit0.map(str => parseFloat(str));
const strToNum1 = doubleArraySplit1.map(str => parseFloat(str));
console.log(strToNum0);
console.log(strToNum1);

正如您在下面的屏幕截图中看到的,它将打印两个数组的最终结果,每个数组都带有实际数值而不是字符串。一旦您使用实数而不是“INF”,NaN 应该消失,它可以从字符串转换为数值。

【讨论】:

    【解决方案3】:

    由于您的模式是预先确定的,并且您不需要灵活的解决方案来在多种情况下工作,因此您的解决方案是可以的。你很接近。

    首先您必须删除 {[ 和 ]} 然后使用 split("], [") 将其拆分为两部分,最后使用 split(", ") 将字符串的 2 部分放入数组中

    var distribution_bins = "{[0.0, 0.3, 0.6, 0.9, 1.2, 1.5, 1.8, 2.1, 2.4, 2.7, +INF], [0.0, 0.12, 0.24, 0.36, 0.48, 0.6, 0.72, 0.84, 0.96, +INF]}";
    
    // Remove {[ and ]}
    distribution_bins = distribution_bins.replace("{[", "");
    distribution_bins = distribution_bins.replace("]}", "");
    
    // split string to 2 seperate parts of your arrays
    var [array1, array2] = distribution_bins.split('], [');
    
    // Make first part of string to array 1
    array1 = array1.split(", ");
    
    // Make second part of string to array 2
    array2 = array2.split(", ");
    
    console.log(array1, array2);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-26
      • 2019-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多