1

Every 2 seconds, I send out a POST that returns:

[{"a":false,"i":"58","b":"sergio","p":"0,22","t":15},
{"a":false,"i":"59","b":"sergio","p":"0,23","t":15},
{"a":false,"i":"60","b":"sergio","p":"0,14","t":15},
{"a":false,"i":"61","b":"sergio","p":"0,07","t":15}]

I have a Javascript function that captures this information and I'd like it to also parse the JSON and place the values into their appropriate elements.

function updateauctions(response) {
    //Parse JSON and place into HTML elements.

    //For debug purposes.
    alert(response);
}

I'm using jQuery and I'd like to know how to fetch JSON values, and place them into HTML elements.

An example of a div .auctionbox:

<div id="60" class="auctionbox gold">
    //Other things here.
</div>

For example, I have n amount of divs with class .auctionbox and each one of those divs also has an ID of #i. So each JSON object corresponds to a single .auctionbox. How would I place the p value of the JSON into the appropriate #i, .auctionbox?

With this simple example, I can start working on other requirements, I just need a little nudge. :)

Thank you.

7 Answers 7

5

http://jsfiddle.net/mA4Ye/4/

Solution with using the template if you need to perform some specific html + css

html

<div id="data">
    <div class="template auctionbox"></div>
</div>

js

var json = '[{"a":false,"i":"58","b":"sergio","p":"0,22","t":15},' +
    '{"a":false,"i":"59","b":"sergio","p":"0,23","t":15},' +
    '{"a":false,"i":"60","b":"sergio","p":"0,14","t":15},' +
    '{"a":false,"i":"61","b":"sergio","p":"0,07","t":15}]';

var data = $.parseJSON(json);
var $template = $('.template');

$(data).each(function() {
    var $element = $template.clone().removeClass('template').appendTo('#data');
    $element.attr('id', this.i);
    $element.html(this.p);
});
Sign up to request clarification or add additional context in comments.

1 Comment

Your answer is very well written and tech agnostic. I love it!
1
setInterval(function() {
    $.getJSON('your-json-url', function(json) {
        $(json).each(function() {
            $('#'+this.i).html(this.p);
        });
    });
}, 2000);

hope this helps

2 Comments

And how would I invoke that getJSON every 2 seconds? Currently I have a generated hidden link that's .Click()'d every 2 seconds using a javascript timer.
if you use var my_interval = setInterval ... you can also use clearInterval(my_interval) to stop the whole process
0
var obj = $.parseJSON(data); // data is the returned JSON string
for( var i in obj){
    $('.auctionbox'+i).html( obj[i].a );
}

That would add the contents of obj.a into a div with the class .auctionbox0, auctionbox1, etc.

Also, I'm not sure you an use numbers as IDs on HTML elements.

Comments

0

You could do:

var response = [{"a":false,"i":"58","b":"sergio","p":"0,22","t":15},
{"a":false,"i":"59","b":"sergio","p":"0,23","t":15},
{"a":false,"i":"60","b":"sergio","p":"0,14","t":15},
{"a":false,"i":"61","b":"sergio","p":"0,07","t":15}];

function updateauctions(response) {
    for (i=0; i<response.length; i++){
    //if the ids are unique just leave the class alone and select the div with the id
    var id = 'div#'+response[i].i;

    $(id).html(response[i].p);
    //For debug purposes.
    }
}

Comments

0

assuming the json response is already parsed to a js object (using dataType: "json" with $.ajax() or setting the optional 4th parameter to "json" with $.get() or $.post()), you can loop through the json array using $.each(), then navigate to each div to set it's value.

$.each(response,function(i,item) {
  $("#id-" + item.i).html(item.p);
});

Note: id's cannot start with a number, they must start with an alpha character. because of this, i assumed you would add id- to the id's.

Edit: here's how i would suggest doing it every 2 seconds:

var timer;
(function ajaxInterval(){
  $.getJSON(url,data,function(){
    timer = setTimeout(function(){
      ajaxInterval();
    },2000);
  });
})();

Comments

0

I think you want something like this inside your function:

$.each(response, function(key, item) {
    $('#' + item.i).text(item.p);
});

Comments

0

I second Samich's answer and say use a templating engine that specializes in converting json to html like json2html

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="http://json2html.com/js/jquery.json2html-2.3-min.js"></script>

<div id='list'></div>

<script>

var json =
[{"a":false,"i":"58","b":"sergio","p":"0,22","t":15},
{"a":false,"i":"59","b":"sergio","p":"0,23","t":15},
{"a":false,"i":"60","b":"sergio","p":"0,14","t":15},
{"a":false,"i":"61","b":"sergio","p":"0,07","t":15}];

//Transform used to create the div
var transform = 
    {tag:'div',id:'.i',class:'actionbox gold',children:[
        {tag:'span',html:'.b'},
        {tag:'span',html:' + other stuff'}
    ]};

//Render the json into a list of divs
$('#list').json2html(json, transform);
</script>

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.