JSON.parse没有预期的行为 [英] JSON.parse not having the expected behaviour

查看:77
本文介绍了JSON.parse没有预期的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过发送来获取 json 请求,然后执行 JSON.parse 就可以了。但是会发生以下错误:


未捕获的SyntaxError:JSON位置2的JSON中出现意外的令牌m $ J $。barse()$ b的b
at:1:19


以下代码再现错误:

  const string ='{msg_reject:\'Rejeitado porque sim \',接受:1,照片:\'FSADKJK23B1 \'}'
const json = JSON.parse(string)

这就是我在<$ c $中发送它的方式c>发布



{msg_reject:'Rejeitado porque sim',接受:1,照片:'FSADKJK23B1'}



我发送的方式有问题吗?

解决方案

您的JSON字符串格式不正确,您必须为键和值添加双引号,因为:

  const string ='{msg_reject:Rejeitado porque sim,accept:1,photo:FSA DKJK23B1}'; 

有许多在线解析器可供您验证JSON字符串,我通常使用 http://json.parser.online.fr/ 在需要时验证我的JSON。 / p>

I'm trying to get a json request, sent by post, and do the JSON.parse on it. But this error happens:

Uncaught SyntaxError: Unexpected token m in JSON at position 2 at JSON.parse () at :1:19

The code below reproduces the error:

const string = '{ msg_reject: \'Rejeitado porque sim\', accept: 1, photo: \'FSADKJK23B1\' }'
const json = JSON.parse(string)

And that's the way I'm sending it in my post

{ msg_reject: 'Rejeitado porque sim', accept: 1, photo: 'FSADKJK23B1' }

Is there something wrong in the way I'm sending it?

解决方案

Your JSON string is not formatted correctly, you will have to add double quotes " for keys & values as:

const string = '{ "msg_reject": "Rejeitado porque sim", "accept": 1, "photo": "FSADKJK23B1" }';

There are many online parser available where you can validate your JSON string, I generally use http://json.parser.online.fr/ to verify my JSON whenever it requires.

这篇关于JSON.parse没有预期的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆