简体   繁体   English

是否有 function 将字符串转换为 Tabulator.js 结构数据的数组?

[英]Is there a function to convert a string to array for Tabulator.js structure data?

I created a Google Application Script that from a sheet generates data for Tabulator.js.我创建了一个 Google 应用程序脚本,它从工作表为 Tabulator.js 生成数据。 As a string.作为一个字符串。 When I copy the text and place it into my html file it works but I load it via Google Appliacation Script call and such string I assign to Tabulator data variable I get an error Data Loading Error - Unable to process data due to invalid data type Expecting: array, Received: string当我复制文本并将其放入我的 html 文件时,它可以工作,但我通过 Google 应用程序脚本调用加载它,并且我分配给 Tabulator 数据变量的此类字符串出现错误Data Loading Error - Unable to process data due to invalid data type Expecting: array, Received: string

Is there any simple way to convert such string to needed array of JSON structure?是否有任何简单的方法可以将此类字符串转换为所需的 JSON 结构数组? Note the children for the first row.注意第一行的children You can play with it in jsFiddle您可以在jsFiddle中使用它

I tried to use JSON.parse but it looks like the string structure is not good SyntaxError: JSON.parse: expected property name or '}' at line 1 column 2 of the JSON data"我尝试使用JSON.parse但看起来字符串结构SyntaxError: JSON.parse: expected property name or '}' at line 1 column 2 of the JSON data"

[
  {id:1, name:"BalanceOil", _children:
    [
      {id:2, name:"BalanceOil+  s testy", cena:31},
      {id:3, name:"BalanceOil+ ", cena:31}
    ]
  },
  {id:11, name:"Xtend x2 Kit", cena:23},
  {id:18, name:"Viva+ Kit", cena:21}
]

The original data look like below but I simplified it.原始数据如下所示,但我对其进行了简化。

[{id:1, name:"BalanceOil", _children:
    [{id:2, name:"BalanceOil+  s testy", cena:31, mn:1,cena_1:"", package:159, kredityMesicne:4, kredityBalik:14},
     {id:3, name:"BalanceOil+ ", cena:31, mn:1,cena_1:"", package:85, kredityMesicne:4, kredityBalik:8},]},
 {id:11, name:"Xtend x2 Kit", cena:23, mn:1,cena_1:"", package:83, kredityMesicne:3, kredityBalik:8},
 {id:18, name:"Viva+ Kit", cena:21, mn:1,cena_1:"", package:60, kredityMesicne:3, kredityBalik:4}
]

Is it possible to use the source of data as a string or I have to change the code a create the structure as the desired object?是否可以将数据源用作字符串,或者我必须更改代码并将结构创建为所需的 object? What would it actually be?实际上会是什么? an array or JSON?一个数组还是 JSON?

Multiple ways to achieve this实现这一目标的多种方法

  1. eval although it could be VERY VERY dangerous if the content passed inside it is not 100% trustworthy as this is simply dynamic code injection/evaluation. eval虽然如果其中传递的内容不是100% 可信赖的,因为这只是动态代码注入/评估,它可能非常非常危险。

 // from your jsfiddle var asTextOriginal ='[{id:1, name:"BalanceOil", _children:[{id:2, name:"BalanceOil+ s testy", cena:31, mn:1,cena_1:"", package:159, kredityMesicne:4, kredityBalik:14},{id:3, name:"BalanceOil+ ", cena:31, mn:1,cena_1:"", package:85, kredityMesicne:4, kredityBalik:8},]},{id:11, name:"Xtend x2 Kit", cena:23, mn:1,cena_1:"", package:83, kredityMesicne:3, kredityBalik:8},{id:18, name:"Viva+ Kit", cena:21, mn:1,cena_1:"", package:60, kredityMesicne:3, kredityBalik:4}]'; eval(asTextOriginal);

  1. manually parse whole string and create array/object out of it: String to object in JS手动解析整个字符串并从中创建数组/对象: String to object in JS

  2. configure the script to return an actual JSON instead string配置脚本以返回实际的 JSON 而不是字符串

PS: the snippet won't run here again because of security reason, though you can try putting it in console PS:由于安全原因,该代码段不会再次在此处运行,但您可以尝试将其放在控制台中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM