简体   繁体   English

无法对来自SQL Server的数据执行JSON.parse

[英]Can't do JSON.parse on data from SQL Server

I have a query that gets data from SQL Server, and in one of the fields we are storing JSON as a varchar(max) . 我有一个从SQL Server获取数据的查询,在一个字段中,我们将JSON存储为varchar(max) The issue is that when I try to get the data out in my Node app and do a JSON.parse , it's not working. 问题是,当我尝试在Node应用程序中获取数据并执行JSON.parse ,它不起作用。 Here's what I've tried: 这是我尝试过的:

console.log(data.attr);
    // "{ firstName: 'Preston', lastName: 'Lamb' }"
JSON.parse(data.attr) 
    // Invalid expression: unexpected end of input (get this on one item)
    // invalid expression: unexpected token f in json at position 2 (and this on the other)
newJsonStr = JSON.stringify(data.attr)
    // ""{ firstName: 'Preston', lastName: 'Lamb' }""
newJson = JSON.parse(newJsonStr)
    // "{ firstName: 'Preston', lastName: 'Lamb' }"

在此处输入图片说明

None of this is really complicated stuff...it should be easy to JSON.parse and/or JSON.stringify, but it doesn't work. 这些都不是真正复杂的东西……JSON.parse和/或JSON.stringify应该很容易,但是它不起作用。 Any ideas at all? 有什么想法吗?

What the other commentator meant by it's not JSON is that your keys need to be wrapped in quotes to be parsed as JSON. 另一个注释器不是JSON的意思是,您的密钥需要用引号引起来以解析为JSON。

JSON Spec - does the key have to be surrounded with quotes? JSON Spec-密钥是否必须用引号引起来?

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

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