简体   繁体   English

Javascript解析JSON对象

[英]Javascript parsing json object

As you can see based on the columns from the excel one column has "space" which is the Categorized Options that when being parsed the 'Categorized Options': was mixed to the Options key as you can see on the json data below. 如您所见,基于excel中的列,一列具有“空格”,即“分类选项”,在解析“分类选项”时:已混合到Options键,如以下json数据所示。 Categorized Options is the only column key that has space. 分类选项是唯一具有空格的列键。 The Categorized Options should be a seperate key. 分类选项应为单独的键。 Is there a way we can alter the paparse json result below to seperate the Categorized Options: key and value to get the desired result ? 有没有一种方法可以更改下面的paparse json结果,以分隔“分类的选项”:键和值以获得所需的结果?

Desired Output - The ouput i wanna get 所需的输出-我想要得到的输出

 [{ Type: 'New',   
  Stock: 'P10092',
  VIN: '1G1YY34U455128500',    
  Year: '2005',
  Options:
   ' Switch',
  Categorized Options:
  'LICENSE PLATE BRACKET',
   ImageList:
   'http: image.com',
  Comment: '2005 CHEVY CORVETT',
  FuelType: 'Gasoline Fuel',
  DriveType: 'RWD' }]

Excel columns Excel栏

Type | VIN | Stock | Year | Options | Categorized Options | ImageList | Comment | FuelType | DriveType
New    | 1G..  | P10092   |  2005   |  Switch   | LICENSE PLATE BRACKET | http: image.com 2005  | comment test  | Gasoline Fuel | RWD

Using Paparse (The data result is now ) 使用Paparse(数据结果现在为)

this is the result after paparse the 'Categorized Options': was being joined to the Options , the output i want is the data above i posted. 这是在稀疏了“分类的选项”之后的结果:已被加入选项,我想要的输出是我发布的上面的数据。 Categorized Options should be a seperate key 分类选项应为单独的键

[{ Type: 'New',   
  Stock: 'P10092',
  VIN: '1G1YY34U455128500',    
  Year: '2005',
  Options:
   ' Switch',
   'Categorized Options':
   'LICENSE PLATE BRACKET,',
   ImageList:
   'http: image.com',
  Comment: '2005 CHEVY CORVETT',
  FuelType: 'Gasoline Fuel',
  DriveType: 'RWD' }

Make sure to set the delimiter to | 确保将delimiter设置为| . You can also use transform and transformHeader to trim the values. 您还可以使用transformtransformHeader修剪值。

More informations on PapaParse docs . 有关PapaParse文档的更多信息。

 const dataText = document.getElementById('data').textContent.trim(); const parsed = Papa.parse(dataText, { header: true, delimiter: '|', transformHeader: header => header.trim(), transform: value => value.trim() }); document.querySelector('#output').innerHTML = JSON.stringify(parsed, null, 2); 
 <script src="https://www.papaparse.com/resources/js/papaparse.js"></script> <h3>Data</h3> <pre id="data"> Type | VIN | Stock | Year | Options | Categorized Options | ImageList | Comment | FuelType | DriveType New | 1G.. | P10092 | 2005 | Switch | LICENSE PLATE BRACKET | http: image.com 2005 | comment test | Gasoline Fuel | RWD </pre> <h3>Output</h3> <pre id="output"> </pre> 

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

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