[英]JSON.parse not creating a json object in javascript
I am getting below string from upstream. 我从上游得到下面的字符串。 Have no control over it. 无法控制它。
b"{'text': 'Airtel Tower (@ BT Tower in Chasdmzoa, Delhi)', 'sentiment': '0.25'}"
I want to change the string to JSON. 我想将字符串更改为JSON。 So the first thing I did was removing preceeding b'....' (upstream is a python program that produces utf-8 string as an output). 所以我要做的第一件事是删除前面的b'....'(上游是一个生成utf-8字符串作为输出的python程序)。 So I am removing 'b' using substr.. 所以我要用substr删除'b'。
str = msg.payload.substr(1);
Then I am trying to convert the string to JSON using JSON.stringify and JSON.parse . 然后,我尝试使用JSON.stringify和JSON.parse将字符串转换为JSON。
console.log(typeof(str));
var t = JSON.stringify(str);
console.log(typeof(t));
var t = JSON.parse(t);
console.log("First: " + t);
var t = JSON.parse(t);
console.log("Second " + t);
x = t.text;
y = t["text"];
console.log(x + " ---- " + y);
Console Output: 控制台输出:
string
string
First: "{'text': 'Airtel Tower(@ KT Tower in Bang, Greater K
n), 'sentiment': '0.25'}"
Second {'text': 'Vodafone Tower (@ LT Tower in Delhi, Greater K
), 'sentiment': '0.25'}
undefined ---- undefined
It fails to convert it to object even though JSON.stringify removed the extra quotes etc. and JSON.parse doesn't seem to work. 即使JSON.stringify删除了多余的引号等,它也无法将其转换为对象,并且JSON.parse似乎不起作用。 What am I doing wrong? 我究竟做错了什么?
There's an issue here: single-quoted string literals aren't valid JSON. 这里有一个问题:单引号字符串文字不是有效的JSON。 JSON is based on JavaScript, but it's not the same thing. JSON基于JavaScript,但这不是一回事。 If you're writing an object literal inside JavaScript code, fine; 如果要在JavaScript代码中编写对象文字,可以; if you actually need JSON, you need to use ". 如果您确实需要JSON,则需要使用“。
const input = "b'\\"{'text': 'Airtel Tower (@ BT Tower in Chasdmzoa, Delhi)', 'sentiment': '0.25'}\\"'"; const cleanString = str => str.split('"')[1].replace(/'/g, '"'); console.log(input); // Result console.log(JSON.parse(cleanString(input)));
"
as a delimiter 用双引号 "
作为分隔符分割字符串 {'text': 'Airtel Tower (@ BT Tower in Chasdmzoa, Delhi)', 'sentiment': '0.25'}
这将为您提供{'text': 'Airtel Tower (@ BT Tower in Chasdmzoa, Delhi)', 'sentiment': '0.25'}
'
with double quotes "
. This will give you {"text": "Airtel Tower (@ BT Tower in Chasdmzoa, Delhi)", "sentiment": "0.25"}
用双引号 "
替换 单引号 '
。这将为您提供{"text": "Airtel Tower (@ BT Tower in Chasdmzoa, Delhi)", "sentiment": "0.25"}
JSON.parse
on the previous string to have your object 在前一个字符串上调用JSON.parse
以使您的对象 Simplest answer (for non-anti- eval
-zealots): 最简单的答案(非抗eval
-zealots):
var string = msg.payload.substring(3,msg.payload.length-2);
var t = eval("(" + string + ")");
If you can't stand, or can't use (strict mode?), eval
, then you need to convert the single quotes to double quotes for parsing, as JSON only accepts double quotes: 如果您不能忍受或不能使用(严格模式?) eval
,则需要将单引号转换为双引号进行解析,因为JSON仅接受双引号:
var string = msg.payload.substring(3,msg.payload.length-2);
var t = JSON.parse(string.replace(/'/g, "\""));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.