0

I know this types of questions are in abundance but a little help would be nice.

So, I have a JSON as following-

 <input id='JSONdata' type='hidden' value='[
 {
 "lat": 40.741895,
 "lng": -73.989308,
 "description": "<div class=\"map-popup\"><figure><img src=\"/images/default-source/default-album/img-000119d0a79213e34d3a8fba81594be76eb9.tmb-devthumb.jpg?sfvrsn=9998b2ca_1\"><figcaption><h4>\"Bowland house 3\"</h4><h5>\"\"</h5><a href=\"\" class=\"btn btn-success\">Find out more</a></figcaption></figure>/div>",
 "icon": "/sf_images/ico-0005.svg"
 },
 {
 "lat": 40.741895,
 "lng": -73.989308,
 "description": "<div class=\"map-popup\"><figure><img src=\"/images/default-source/default-album/img-000119d0a79213e34d3a8fba81594be76eb9.tmb-devthumb.jpg?sfvrsn=9998b2ca_1\"><figcaption><h4>\"Bowland house\"</h4><h5>\"\"</h5><a href=\"\" class=\"btn btn-success\">Find out more</a></figcaption></figure>/div>",
 "icon": "/sf_images/ico-0005.svg"
 }
]' />

This is generated from asp.net and written on the page using-

Response.Write(string.Concat("<input id='JSONdata' type='hidden' value='", json, "' />"));

Following the answer written here. I tried to read the value by doing

var jq = $.noConflict();
var json2 = jq('#JSONdata');
var JSONData = JSON.parse(json2.value);

But I get the following error-

enter image description here

And unable to read the value, left me with my head scratching....

Any ideas?

asked May 5, 2017 at 20:46
3
  • What happens if you escape all the double quotes by replacing " with &quot; within your html? Commented May 5, 2017 at 21:13
  • Well it's coming from asp.net code can't really build it that way as double quote and single are two different things apparently. Just learned! Commented May 5, 2017 at 21:24
  • What does this have to do with C# or asp.net? Commented May 5, 2017 at 22:38

2 Answers 2

1

Replace your line

var JSONData = JSON.parse(json2.value);

with

var JSONData = JSON.parse(json2[0].value);
answered May 5, 2017 at 21:46
1
  • Thanks Krypton.... that returns correct json now :). On the side note, if i were to change part of double quotes in the json, how would i go about and do it? like i want the div's inside the json to be wrapped in single quote. "<div class=\"map-popup\"></div>" To '<div class=\"map-popup\"></div>' Commented May 5, 2017 at 21:50
0

It means your variable json2.value is undefined. You're passing undefined into JSON.parse, and since the first letter is u, it says 'Unexpected token u'.

answered May 5, 2017 at 20:59
3
  • On debug console I can actually see that var json2 has value so why is json2.value undefined? Commented May 5, 2017 at 21:09
  • What happens when you just console.log(json2.value)? Commented May 5, 2017 at 21:11
  • I get Uncaught ReferenceError: json2 is not defined at <anonymous>:1:13 Commented May 5, 2017 at 21:26

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.