javascript - JSON.parse returning undefined on my string - Stack Overflow

I have a returned string from my server and I want to parse it into a JSON object, the following is the

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745265275a4619416.html

相关推荐

  • javascript - JSON.parse returning undefined on my string - Stack Overflow

    I have a returned string from my server and I want to parse it into a JSON object, the following is the

    13小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信