简体   繁体   English

在JavaScript中修复此JSON对象字符串的最干净的方法是什么?

[英]Whats the cleanest way to repair this JSON object string in javascript?

I am doing some site crawling, and right now, I find this string of a JSON object 我正在进行一些网站爬网,现在,我找到了JSON对象的此字符串

{ "results" : [

    {
        id: 775664,
        status: "In-Stock",
        ffmtCenterId: '10601',
        altText: "In-Stock",
        qty: 6
    }

]}

and I wan't to convert that to a object using JSON.parse() but this doesn't work because there are syntax errors, like you need quotes around the key values. 并且我不希望使用JSON.parse()将其转换为对象,但是由于存在语法错误(例如您需要在键值周围加上引号JSON.parse() ,因此无法正常工作。 I tried eval() but it didn't work. 我尝试了eval()但是没有用。

Does anyone know whats a good way to fix this string so that I can convert it into an object? 有谁知道修复此字符串的好方法,以便可以将其转换为对象?

Thanks 谢谢

how did you try eval() ? 您如何尝试eval()

eval("var a = " + incoming_string);
var fixed = JSON.stringify(a);

a is the object, I don't think you need that fixed string. a是对象,我认为您不需要该fixed字符串。

Of course, eval an unknown string is not safe. 当然,评估未知字符串并不安全。

The hjson project seems to be a possible solution. hjson项目似乎是一个可能的解决方案。 Thier online converter managed to parse your broken structure into proper JSON though there seem to be some quirks in the conversion. 他们的在线转换器设法将损坏的结构解析为正确的JSON,尽管在转换中似乎有些古怪。

Though I see you've commented that you've already found a fix, here's a double replace() that worked on this sample as a multiline string: 尽管我看到您评论过您已经找到了修复程序,但是这里有一个double replace()作为多行字符串在此示例上起作用:

var a = `{ "results" : [

    {
        id: 775664,
        status: "In-Stock",
        ffmtCenterId: '10601',
        altText: "In-Stock",
        qty: 6
    }

]}`

var b = a.replace(/(\w+)(: )/g, "\"$1\" $2" ).replace(/(:\s+)['](\w+)['](,\n)/g, "$1\"$2\"$3" )

var jo = JSON.parse( b )

Without the 2nd replace() , I was getting JSON.parse errors in Firebug on the ffmtCenterId data. 没有第二个replace() ,我在ffmtCenterId数据上的Firebug中收到JSON.parse错误。

暂无
暂无

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

相关问题 将JavaScript对象转换为多维数组的最简洁方法 - Cleanest way to convert JavaScript object to multi-dimensional array 基于对象属性的Javascript最干净的方法来区分数组 - Javascript cleanest way to distinct array based on object property 在 JavaScript 中编写排序方法的最简洁方法,该方法对多个 object 属性进行排序 - Cleanest way to write a sort method in JavaScript that sorts on multiple object properties 在数组(或对象)上迭代异步的最聪明/最干净的方法是什么? - Whats the smartest / cleanest way to iterate async over arrays (or objs)? 什么是检查javascript中字符串集合中是否存在字符串的最快方法? - Whats the fastest way to check if a string exists in a string collection in javascript? 我正在使用<script type=“text/json” id=“myJSON”> on the webpage. Whats the best way to access it on the JavaScript? - I am exposing an object using <script type=“text/json” id=“myJSON”> on the webpage. Whats the best way to access it on the JavaScript? 在 JavaScript 中实现 singleton 的最简单/最干净的方法 - Simplest/cleanest way to implement a singleton in JavaScript 可以从Dart调用的JavaScript对象的代理/传递的最简洁方法 - Cleanest way to make a proxy/passthrough for a JavaScript object that can be called from Dart 关闭JavaScript中承诺的连接的最干净的方法是什么 - What is the cleanest way to close promised connection in javascript 最简洁的使用异步的方法导致Javascript - Cleanest way to use async results in Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM