2

I have a data() object containing some json.

Is there a way I can loop through the object and grab each parts key and value?

This is what I have so far:

function getFigures() {

var propertyValue = getUrlVars()["propertyValue"];

$.getJSON(serviceURL + 'calculator.php?value=' + propertyValue, function(data) {

    figures = data.figures;
    $.each(figures, function(index, figure) {

        $('#figureList').append('<li> index = ' + data.figures.figure + '</li>');

    });

});

$('#figureList').listview('refresh');

}

The json looks like this:

{"figures":{"value":"150000","completion":"10.00","coal":"32.40","local":"144.00","bacs":"35.00","landRegistry":"200.00","solFee":"395.00","vatOnSolFees":79,"stampDuty":1500,"total":2395.4}}

Apologies if its simple, I'm new to jQuery and couldn't find anything on SO that helped.

2

3 Answers 3

5

You can get the key and value like this

$.each(data.figures, function(key, val) {

        console.log('Key: ' + key + '  Val: ' + val)

    });​

So change your code to

 $('#figureList').append('<li>'+ index + ' = ' + figure + '</li>');

Demo: http://jsfiddle.net/joycse06/ERAgu/

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

Comments

0

The parameters index and figure contains the parameter name and value. I think that you want to concatenate the parameters into the string:

$('#figureList').append('<li>' + index + ' = ' + figure + '</li>');

An alternative is to create the list item element and set the text of it, that would also work if the text contains any characters that need encoding:

$('#figureList').append($('<li/>').text(index + ' = ' + figure));

2 Comments

Sir, in first one, the starting li have > missing .
@Joy: Yes, you are right, it got lost in the editing. Fixed it. :)
0
function getFigures() {

   var propertyValue = getUrlVars()["propertyValue"];

   $.getJSON(serviceURL + 'calculator.php?value=' + propertyValue, function(data) {

       $.each(data['figures'], function(index, val) {

          here grab "val" is key 

          $.each(data['figures'][index], function(col, valc) {

           here grab "col" is value

          }
       }

   }      

bye

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.