如何从mysql检索相同格式的JSON数据,我的mysql json数据在js中作为字符串接收?

问题描述 投票:0回答:3

如何从Mysql检索相同格式的JSON数据,我的mysql json数据在js中作为字符串接收。

我无法阅读收到的以下内容:

menu: "{"Reports": "off", "Dashboard": "on"}"
angular
3个回答
0
投票
menu: "{"Reports": "off", "Dashboard": "on"}"

不是有效的字符串。但打击是

menu: '{"Reports": "off", "Dashboard": "on"}'

并且您可以像这样将其转换为JSON

var data = '{"Reports": "off", "Dashboard": "on"}'

parsedData = JSON.parse(data)
console.log(parsedData)

1
投票

使用javascript函数JSON.parse(yourdata)将文本转换为javascript对象。

注意:确保文本以JSON格式编写,否则您将收到语法错误。


0
投票

您可以使用JSON.parse()方法将有效的json字符串转换为json

const stringJson = '{"menu":{"reports":"off","Dashboard":"on"}}';
const jsonObj = JSON.parse(stringJson);

console.log(jsonObj);
console.log(jsonObj.menu.Dashboard);
© www.soinside.com 2019 - 2024. All rights reserved.