1

jQuery Code:

$(document).ready(function(){
    $.getJSON('dat.js', function(data) {
         var obj = JSON.parse(data);
         alert(obj[0].title);
    });
});

My JSON file :

{
    "posts": 
    [
        {
        "title": "ajax | Programming ",
        "url": "hello"
        },
        {
        "title": "jQuery and Ajax Demos Pard - 3",
        "url": "how are you"
        },
    ]
}

Its giving me an error JSON.parse:unexpected character. But when I tried to do it by taking the json inside an array its ok then. I want to access the data from json file itself

1
  • It's the extra comma character after the last post object, after "how are you". Commented Mar 21, 2012 at 5:37

2 Answers 2

1

you do parseJSON when your input is a json string and u expect an object. Here, getJSON is already giving u the response as an object.

try this

$(document).ready(function(){

    $.getJSON('dat.js', function(obj) {
         alert(obj.posts[0].title);
        });
    });
Sign up to request clarification or add additional context in comments.

Comments

1

A Quick jslint check says that you have invalid json at line 11 },, Try removing the comma from the last member of "posts" and see if that help

            {
            "title": "jQuery and Ajax Demos Pard - 3",
            "url": "how are you"
            }, <---- THIS

3 Comments

are you able to give me the link to the page's url?
but when i attach the same in json its not working JSON.parse:unexpected charecter.
can you try inspecting the response from chrome's console? See if your server returns any funky characters

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.