Reputation: 71
I am getting this json from server:
{"cust":[{"email":"[email protected]","url":"www.uzti.com"},
{"email":"[email protected]","url":"www.url.com"}]}
I have checked this json on JsonLint site and it shows valid json. But I have retrieve the elements by Javascript. When I do this JSON.parse(json), I get the following error:
SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 1 column 9 of the JSON data. Even if I directly use jaon.cust, it shows undefined How do I Resolve this?
Upvotes: 1
Views: 174
Reputation: 1194
JSON.parse()
parses string to json, if you are getting direct JSON from server then you have no need to parse it , access it directly with property, for example, look at the snippet.
$(document).ready(function(){
var a = {"cust":[{"email":"[email protected]","url":"www.uzti.com"},
{"email":"[email protected]","url":"www.url.com"}]} ;
alert(a.cust[0].email);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Upvotes: 1
Reputation: 173
I think that JSON.parse should use with a string not with a json object.
JSON.parse('{"cust":[{"email":"[email protected]","url":"www.uzti.com"},{"email":"[email protected]","url":"www.url.com"}]}')
try to convert your json to a string and use parse function.
Upvotes: 1