1

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?

7
  • if you do console.log(json) right before the JSON.parse(json), do you get the exact same result as the json data you pasted here? Commented Jan 29, 2016 at 12:18
  • yes, I get the exact same result Commented Jan 29, 2016 at 12:21
  • What length of response from server? Commented Jan 29, 2016 at 12:22
  • did not understand what you meant, but server took 600ms or did you ask something else? Commented Jan 29, 2016 at 12:24
  • console.log(jsonStringFromServer.length) Commented Jan 29, 2016 at 12:28

2 Answers 2

1

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>

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

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.