admin管理员组

文章数量:1417421

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 :

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

I'm getting this as output :

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

so I can't access the male, female, and brand objects within the JSON.

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 :

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

I'm getting this as output :

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

so I can't access the male, female, and brand objects within the JSON.

Share Improve this question asked Jul 10, 2020 at 11:46 Fahd LyousfiFahd Lyousfi 861 silver badge6 bronze badges 2
  • 3 The dataJson is a string – user0101 Commented Jul 10, 2020 at 11:48
  • how could it be,isn't the JSON.parse returns a JSON object ? – Fahd Lyousfi Commented Jul 10, 2020 at 11:49
Add a ment  | 

5 Answers 5

Reset to default 3

correct json to be parsed should be

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.

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

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

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. Remove extra quotation marks and it will think it is an object.

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

Your json string is invalid format. In JSON, keys must be strings written with double quotes not in single quotes. e.g. {"male":16}. Try to read this https://www.w3schools./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)

I could solve this problem by using:

dataJson  = JSON.parse(decodeURI(stringToParse));

here is a reference to it: https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURI

本文标签: javascriptJSONparse returning undefined on my stringStack Overflow