sindhuja Thiyagarajan
sindhuja Thiyagarajan

Reputation: 35

Convert the string into dictionary value in jquery and json

I need to convert the below string "{'id':1,'name':'Sindhuja','address':'Coimbatore'}" into dictionary in jquery.

Upvotes: 0

Views: 4849

Answers (2)

Vinod
Vinod

Reputation: 328

var obj = JSON.parse(str.replace(/'/g, '"'));

Just convert it into an Object.

Upvotes: 1

Neil
Neil

Reputation: 14321

JSON.parse is built into JavaScript. Additionally, JSON only accepts ", not '; so, I added a replace function call.

<script>
di = JSON.parse("{'id':1,'name':'Sindhuja','address':'Coimbatore'}".replace(/'/g,"\""));
console.log(di);
</script>

Upvotes: 2

Related Questions