小编Nit*_*tro的帖子

为什么json.parse会中断?以及如何解决

我有以下数据从服务器输入到服务器的js代码中。

{"triggers": [{"message_type": "sms","recipients": "[\"+91xxxxxxxxx\",\"+91xxxxxxxxx\"]","message": "This is a test"}]}
Run Code Online (Sandbox Code Playgroud)

我的代码以以下方式解析上述json字符串。

data = '{"triggers": [{"message_type": "sms","recipients": "[\"+91xxxxxxxx\",\"+91xxxxxxxx\"]","message": "This is a test"}]}'
parsed = JSON.parse(data);
Run Code Online (Sandbox Code Playgroud)

这引发以下异常

Uncaught SyntaxError: Unexpected token + in JSON at position 54
    at JSON.parse (<anonymous>)
    at eval (eval at <anonymous> (entry.html:2), <anonymous>:1:6)
    at entry.html:298
Run Code Online (Sandbox Code Playgroud)

我做了一些进一步的挖掘,发现了json字符串的来源。这是我的python代码输入的字符串

data = {"recipients": "[\"+91xxxxxxxxx\",\"+91xxxxxxxx\"]"} # This data comes in from my database, and I can't do anything about what quotes are used.
javascript_supplied_data = json.dumps(data) #This data goes to the frontend via webhook …
Run Code Online (Sandbox Code Playgroud)

javascript python

0
推荐指数
1
解决办法
79
查看次数

标签 统计

javascript ×1

python ×1