简体   繁体   English

JSON.parse 在我的字符串上返回 undefined

[英]JSON.parse returning undefined on my string

I have a returned string from my server and I want to parse it into a JSON object, the following is the string and what I am doing:我有一个从我的服务器返回的字符串,我想将它解析为 JSON object,以下是字符串和我在做什么:

stringToParse = "\"{'female': 16, 'brand': 75, 'male': 8}\""
dataJson = JSON.parse(stringToParse)
console.log(dataJson)
console.log(dataJson.male)

I'm getting this as output:我得到的是 output:

{'female': 16, 'brand': 75, 'male': 8}
undefined

so I can't access the male, female, and brand objects within the JSON.所以我无法访问 JSON 中的男性、女性和品牌对象。

There is something wrong with you string I think those extra quotation marks are useless.你的字符串有问题我认为那些额外的引号没用。 The JSON parser things that you providing him with a string "{"female": 16, "brand": 75, "male": 8}" and it parses it as string so you see the console.log result {"female": 16, "brand": 75, "male": 8} but it is not an objet the whole thing is a string. JSON 解析器向他提供字符串 "{"female": 16, "brand": 75, "male": 8}" 并将其解析为字符串,因此您会看到 console.log 结果{"female": 16, "brand": 75, "male": 8}但它不是一个对象,整个东西都是一个字符串。 Remove extra quotation marks and it will think it is an object.去掉多余的引号,它会认为它是一个 object。

 stringToParse = '{"female": 16, "brand": 75, "male": 8}' dataJson = JSON.parse(stringToParse) console.log(dataJson) console.log(dataJson.male)

correct json to be parsed should be要解析的正确 json 应该是

stringToParse = '{"female": 16, "brand": 75, "male": 8}'

you need to alter the code at your server to return data in this manner, or handle it in your js file.您需要更改服务器上的代码以这种方式返回数据,或者在您的 js 文件中处理它。

This code works此代码有效

let stringToParse = '{"female": 16, "brand": 75, "male": 8}'
dataJson = JSON.parse(stringToParse)
console.log(dataJson)
console.log(dataJson.male)

If, though, you can't alter the original stringToParse , then try this in order to parse it at js但是,如果您无法更改原始stringToParse ,请尝试此操作以便在 js 中解析它

let stringToParse = "\"{'female': 16, 'brand': 75, 'male': 8}\""
JSON.parse(stringToParse.replace(/\"/g, '').replace(/'/g, '"'))

Your json string is invalid format.您的 json 字符串格式无效。 In JSON, keys must be strings written with double quotes not in single quotes.在 JSON 中,键必须是用双引号而不是单引号编写的字符串。 eg {"male":16}.例如{“男性”:16}。 Try to read this https://www.w3schools.com/js/js_json_syntax.asp .尝试阅读此https://www.w3schools.com/js/js_json_syntax.asp So below is the correct answer:所以下面是正确答案:

stringToParse = '{"female": 16, "brand": 75, "male": 8}'
dataJson = JSON.parse(stringToParse)
console.log(dataJson)
console.log(dataJson.male)

In your second line parse like this在你的第二行解析这样

stringToParse = "\"{'female': 16, 'brand': 75, 'male': 8}\""
dataJson = JSON.parse(stringToParse.replace(/\"/g, '').replace(/'/g, '"'))

 stringToParse = "\"{'female': 16, 'brand': 75, 'male': 8}\"" dataJson = JSON.parse(stringToParse.replace(/\"/g, '').replace(/'/g, '"')) console.log(dataJson) console.log(dataJson.male)

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

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