0

I am trying to parse a JSON object (not stored on a file) by using $.ajax() method but it is not functioning. What am I doing wrong?

var painting = [
        {
        "title": "Boardwalk 5",
        "artist": "Arnie Palmer",
        "image": "ap1.jpg",
        "price": 850
        },
        {
        "title": "A Lasting Piece",
        "artist": "Arnie Palmer",
        "image": "ap2.jpg",
        "price": 450
        },
        {
        "title": "Surf at High Tide",
        "artist": "Arnie Palmer",
        "image": "ap3.jpg",
        "price": 950
        },
        {
        "title": "The Games We Play",
        "artist": "Arnie Palmer",
        "image": "ap4.jpg",
        "price": 850
        }
      ];
$(document).ready(function () {
$.ajax({
    type: 'GET',
    url: 'painting',
    dataType: 'json',
    success: jsonParser
});

});

function jsonParser(json) {
    $.getJSON(painting, function(data){
        $.each(painting, function(k,v){
            var title = v.title;
            var price = v.price;
            $('#container').append('<div class="painting"><br/><div class="title">' + title + '<br/>$' + price + '</div></div>')
        });
    });
}
4
  • 2
    Why are you passing an Array instead of a URL to $.getJSON? And you seem to be ignoring the json returned form the $.ajax call. Why is that? Commented Apr 3, 2014 at 22:09
  • What do you expect $.getJSON(painting to do? $.getJSON is a AJAX method. painting is already an object, it's NOT a JSON string! Just get rid of $.getJSON(painting,. Commented Apr 3, 2014 at 22:10
  • Ok I got confused! is this "painting" JSON or Not!? As far as I know it is not Javascript Object because of having "" in both key and value pair. so wjy are you guys saying to not use the Ajax method! Commented Apr 3, 2014 at 22:28
  • @user1760110: You mean the painting variable? No, it's not referencing JSON data. If it were JSON data, it would be simple text that represents a data structure. Sort of like XML is textual data representing a data structure. Within your program, the painting variable will be referencing a JavaScript Array of Objects. Commented Apr 4, 2014 at 2:33

2 Answers 2

2

Why do you need .ajax to parse json that is in variable?

Have you tried only this:

$.each(painting, function(k,v){
  var title = v.title;
  var price = v.price;
  $('#container').append('<div class="painting"><br/><div class="title">' + title + '<br/>$' + price + '</div></div>')
});

If I understood you good, you do not need .getjson and .ajax since those are Ajax calls to retrieve external data?

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

2 Comments

"to parse json that is in variable" It's not even JSON, it's an array of objects.
True that, but in the spirit of the question, it "looks" like json.
0

Maybe he's trying to get a json from a file ?

Maybe painting.json or painting is his json file. that'll explain why he's using ajax and getJson.

You have to understand that $.getJSON uses $.ajax.

So :

var painting = [
    {
    "title": "Boardwalk 5",
    "artist": "Arnie Palmer",
    "image": "ap1.jpg",
    "price": 850
    },
    {
    "title": "A Lasting Piece",
    "artist": "Arnie Palmer",
    "image": "ap2.jpg",
    "price": 450
    },
    {
    "title": "Surf at High Tide",
    "artist": "Arnie Palmer",
    "image": "ap3.jpg",
    "price": 950
    },
    {
    "title": "The Games We Play",
    "artist": "Arnie Palmer",
    "image": "ap4.jpg",
    "price": 850
    }
  ];
  $(document).ready(function () {

    $.getJSON('painting.json', function(data){
        $.each(data, function(k,v){
            var title = v.title;
            var price = v.price;
            $('#container').append('<div class="painting"><br/><div class="title">' + title  + '<br/>$' + price + '</div></div>')
        });

     });
});

is enough

2 Comments

Thanks Su4P, Honestly it was captured from a file json data but as you can see I have updated it to be formatted as jason object.The reason that I asked this question is what if I have all of this data stored inside a variable? Then how I can achive them?
You can archive the data in the database as json if you are in php you can do json_encode($_POST) to have json again or if you want you can store them as serialize array. You also can store the data in a file but I wouldn't recommand it. Either way check json_encode and serialize function. If you are not in PHP there are equivalent in most of the language. As my guess was right maybe you can vote up ?

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.