0

i am learning JSON. i just write a simple code which is not working and also not giving any error when clicking on button. so please help me to catch the error. here is my html.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

    <script type="text/javascript" language="javascript">
        function GetJSON() 
        {
            alert("pp");

            var moviereviewtext = "{"title": "Friday the 13th", "year": 1980, "reviews": 
            [{"reviewer": "Pam", "stars": 3, "text": "Pretty good, but could have used more Jason"}, 
            {"reviewer": "Alice", "stars": 4, "text": "The end was good, but a little unsettling"}]}";
            var jsonobj = eval("(" + moviereviewtext + ")");

            var reviewername = jsonobj.reviews[0].reviewer;
            var numberstars = jsonobj.reviews[0].stars;
            var reviewerthoughts = jsonobj.reviews[0].text;

            alert(reviewername);
            alert(numberstars);
            alert(reviewerthoughts);
        }
    </script>
</head>
<body >
    <form id="form1" runat="server">
        <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="GetJSON();return false;" />
    </form>
</body>
</html>

2 Answers 2

1

This is browser dependent but with a few small changes:

// Use single quote to surround and line continuations to support new lines.
var movieReviewText = 
    '{"title": "Friday the 13th", "year": 1980, "reviews":\
    [{"reviewer": "Pam", "stars": 3, "text": "Pretty good, but could have used more Jason"},\ 
    {"reviewer": "Alice", "stars": 4, "text": "The end was good, but a little unsettling"}]}';

var obj = JSON.parse(movieReviewText); // This should be available IE8+, FF3+

Also, take note that a function that starts with a capital letter is by convention considered a constructor. The name of your function also could be made better as the function does not actually get JSON. Maybe something like this?

function parseMovieReview() {


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

Comments

1

I think the problem is in the ASP code. Try leaving ASP aside. Following code works for me:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>

    <script type="text/javascript" language="javascript">
        function GetJSON() 
        {
            alert("pp");

            var moviereviewtext = '{"title": "Friday the 13th", "year": 1980, "reviews":\
            [{"reviewer": "Pam", "stars": 3, "text": "Pretty good, but could have used more Jason"},\
            {"reviewer": "Alice", "stars": 4, "text": "The end was good, but a little unsettling"}]}';
            var jsonobj = eval("(" + moviereviewtext + ")");

            var reviewername = jsonobj.reviews[0].reviewer;
            var numberstars = jsonobj.reviews[0].stars;
            var reviewerthoughts = jsonobj.reviews[0].text;

            alert(reviewername);
            alert(numberstars);
            alert(reviewerthoughts);
        }
    </script>
</head>
<body >
    <form id="form1">
        <Button ID="Button1" Text="Button" OnClick="GetJSON();return false;" />
    </form>
</body>
</html>

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.