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-
And unable to read the value, left me with my head scratching....
Any ideas?
-
What happens if you escape all the double quotes by replacing " with " within your html?Krypton– Krypton2017年05月05日 21:13:36 +00:00Commented 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!envyM6– envyM62017年05月05日 21:24:14 +00:00Commented May 5, 2017 at 21:24
-
What does this have to do with C# or asp.net?Mad Myche– Mad Myche2017年05月05日 22:38:29 +00:00Commented May 5, 2017 at 22:38
2 Answers 2
Replace your line
var JSONData = JSON.parse(json2.value);
with
var JSONData = JSON.parse(json2[0].value);
-
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>'
envyM6– envyM62017年05月05日 21:50:13 +00:00Commented May 5, 2017 at 21:50
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'.
-
On debug console I can actually see that var json2 has value so why is json2.value undefined?envyM6– envyM62017年05月05日 21:09:12 +00:00Commented May 5, 2017 at 21:09
-
What happens when you just
console.log(json2.value)
?Joey– Joey2017年05月05日 21:11:04 +00:00Commented May 5, 2017 at 21:11 -
I get
Uncaught ReferenceError: json2 is not defined at <anonymous>:1:13
envyM6– envyM62017年05月05日 21:26:55 +00:00Commented May 5, 2017 at 21:26