简体   繁体   English

格式化JSON对象,从单引号到双引号

[英]Formating JSON object, From single quotation to double

I'm using a data-set given to me and I'm trying to parse (Using Node) the JSON objects returned to me,我正在使用给我的数据集,我正在尝试解析(使用 Node)返回给我的 JSON 对象,

Turns out they are all using single quotations, and from my research JSON uses double.原来他们都使用单引号,而我的研究 JSON 使用双引号。

Example of a JSON object I get returned!我返回的 JSON 对象示例!

{
    'cast_id': 16,
    'character': 'Alexander Haig',
    'credit_id': '52fe43c59251416c7501d72d',
    'gender': 2,
    'id': 6280,
    'name': 'Powers Boothe',
    'order': 2,
    'profile_path': '/3nNL6AvMAYq0BmHKM79RnRZVq3i.jpg'
},

I've been using str.replace() to sort the objects out before JSON.parse() which was fine untill I found objects like this one我一直在使用str.replace()JSON.parse()之前对对象进行排序,这很好,直到我找到这样的对象

{
    'cast_id': 26,
    'character': '"Jack Jones"',
    'credit_id': '52fe43c59251416c7501d751',
    'gender': 2,
    'id': 6840,
    'name': 'Larry Hagman',
    'order': 16,
    'profile_path': '/40PVsGp5Wp5kbUhAefLHqjqbarc.jpg'
},

Notice the 'character': '"Jack Jones"', This has been causing me all types of issues!注意'character': '"Jack Jones"',这给我带来了各种各样的问题!

I there a library that will help parse this all for me?我有一个图书馆可以帮助我解析这一切吗?

Am I missing something?我错过了什么吗?

FYI:供参考:

I can't access each record as the JSON objects aren't stored separately instead, as a long string including up to 60 JSON objects.我无法访问每条记录,因为 JSON 对象不是单独存储的,而是作为包含多达 60 个 JSON 对象的长字符串。

数据库中数据集的屏幕截图

I currently have a function that helps parse the data:我目前有一个帮助解析数据的函数:

function formatJSON(cast) {
    cast = cast.replace(/(\w) "(\w)/g, "$1 *$2");
    cast = cast.replace(/(\w)" /g, "$1* ");         
    cast = cast.replace(/': '/g, '": "');
    cast = cast.replace(/', '/g, '", "' );
    cast = cast.replace(/'},/g, '"},');
    cast = cast.replace(/': /g, '": ');
    cast = cast.replace(/, '/g, ', "');
    cast = cast.replace(/{'/g, '{"');
    cast = cast.replace(/: None}/g, ': "None"}');
    cast = cast.replace(/'}/g, '"}');
    return cast;
}

Update更新

The data reportedly extracts nicely in python as a dictionary using ast.literal_eval()据报道,数据在 python 中使用ast.literal_eval()很好地提取为字典

By default, you don't need to replace a string which is wrapped by double quotes with the same string contains single quote.默认情况下,您不需要用包含单引号的相同字符串替换由双引号包裹的字符串。 There is no difference between 'Jack Jones' and "Jack Jones" since all of them are string . 'Jack Jones'"Jack Jones"之间没有区别,因为它们都是string

In your case, you're trying to replace something like this example:在您的情况下,您正在尝试替换以下示例:

 var str = '\\'Jack Jones\\''.replace(/'/g, '"'); console.log('\\'' + str + '\\'');


So, if you want to wrap all of properties names and values by double quotes, you can use JSON.stringify and JSON.parse like this:所以,如果你想用双引号包裹所有的属性名称和值,你可以像这样使用JSON.stringifyJSON.parse

 var cast = { 'cast_id': 16, 'character': 'Alexander Haig', 'credit_id': '52fe43c59251416c7501d72d', 'gender': 2, 'id': 6280, 'name': 'Powers Boothe', 'order': 2, 'profile_path': '/3nNL6AvMAYq0BmHKM79RnRZVq3i.jpg' }; cast = JSON.stringify(cast); cast = JSON.parse(cast); console.log(cast);

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

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