简体   繁体   English

如何解析包含混合字符串和数字的 JSON object 的一部分?

[英]How do I parse part of a JSON object that has mixed string and numbers?

I have a JSON file that was processor generated with lines like this我有一个 JSON 文件,它是处理器用这样的行生成的

jsonData:   "{data: [350.23,250.32,150.34,340.50,236.70,370.45,380.55]}"

I can target the 'jsonData' object but that returns everything within the double quotes as a string.我可以定位'jsonData' object 但这会将双引号内的所有内容作为字符串返回。 I tried...dataset[0].jsonData[8] which returns the '3' from the first value.我试过...dataset[0].jsonData[8] 从第一个值返回“3”。 I guess I could throw the mixed strings into a JS function and use regex to remove the extra stuff, but thats probably the hackyest way to do this.我想我可以将混合字符串放入 JS function 并使用正则表达式删除多余的东西,但这可能是最老套的方法。

Whats the easiest way to target the values only?仅针对值的最简单方法是什么?

If you don't want to spend the time fixing your JSON just do this:如果您不想花时间修复 JSON,请执行以下操作:

let values = "{data: [350.23,250.32,150.34,340.50,236.70,370.45,380.55]}".split(',').map(_ => _.replace(/[^0-9.]/g,''))

console.log(values)

If you want to interact with it like the list I would consider something like如果您想像列表一样与它进行交互,我会考虑类似

var list = jsonData.split("[")[1].split("]")[0].split(",")

Console.log(list);

The console reads:控制台显示:

[
  '350.23', '250.32',
  '150.34', '340.50',
  '236.70', '370.45',
  '380.55'
]

From here you can use list[3] to get 340.50从这里您可以使用 list[3] 获得 340.50

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

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