繁体   English   中英

遍历jquery data()对象以获取键和值

[英]Loop through jquery data() object to get keys and values

我有一个包含一些json的data()对象。

有没有一种方法可以遍历对象并获取每个零件的键和值?

这是我到目前为止的内容:

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');

}

json看起来像这样:

{"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}}

很简单,很抱歉,我是jQuery的新手,所以在SO上找不到任何有帮助的东西。

您可以像这样获得keyvalue

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

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

    });​

因此,将您的代码更改为

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

演示: http //jsfiddle.net/joycse06/ERAgu/

参数indexfigure包含参数名称和值。 我认为您想将参数连接到字符串中:

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

一种替代方法是创建列表项元素并设置其文本,如果文本包含任何需要编码的字符,该方法也将起作用:

$('#figureList').append($('<li/>').text(index + ' = ' + figure));
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

          }
       }

   }      

再见

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM