迭代JSON数组:获取参数的名称 [英] Iterate through JSON array: Get name of the parameter

查看:108
本文介绍了迭代JSON数组:获取参数的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在迭代我的JSON响应。我有id元素的HTML元素作为参数名称。

I'm iterating through my JSON response. I've got HTML elements with id's as the parameter names.

例如,我的JSON响应包含costcenter:1234,并且有一个< span> 元素,其ID为 costcenter

For example, my JSON response contains a "costcenter":"1234", and there's a <span> element with the id costcenter.

现在,我没有为每个id编写语句,而是认为我可以遍历JSON数组,并自动读取其名称。

Now, instead of writing a statement for every id, I thought I could iterate through the JSON array, and automatically read its name.

这就是我得到的

$(".dataset").click(function() {
    changeid = this.id;

    $.ajax({
        url: "source",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        data: {
            id: changeid
        },
        success: function(data) {
            // How to get the name of the parameter, and then read it's value?
        }
    })
})

JSON看起来像这样,它只是一个维度和1个结果集:
{changeid:1,costcenter:478,manager:John Smith}

The JSON looks like that, it's just one dimension and 1 result set only: {"changeid":"1","costcenter":"478","manager":"John Smith"}

推荐答案

如果我理解你的问题,你在成功函数中需要类似的东西:

If I understand your question you want something like that in your success function :

for (var key in data) {
   $('#'+key).html(data[key]);
}

这篇关于迭代JSON数组:获取参数的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆