使用 jQuery 从 json_encode 获取值

发布于 2024-12-06 11:28:11 字数 1338 浏览 0 评论 0原文

我从数据库表中的一行中插入了多个带有 json_encode 的(array)值,现在希望使用 jquery 将它们按顺序回显。

这是我的 PHP 代码的输出:

[{
    "guide": null,
    "residence": [{
        "name_r": "jack"
    }, {
        "name_r": "jim"
    }, {
        "name_r": "sara"
    }],
    "residence_u": [{
        "units": ["hello", "how", "what"],
        "extra": ["11", "22", "33"],
        "price": ["1,111,111", "2,222,222", "3,333,333"]
    }, {
        "units": ["fine"],
        "extra": ["44"],
        "price": ["4,444,444"]
    }, {
        "units": ["thanks", "good"],
        "extra": ["55", "66"],
        "price": ["5,555,555", "6,666,666"]
    }]
}]

我想要 as(output):

杰克
你好 & 11 & 1,111,111
如何 & 22 & 2,222,222
什么 & 33 & 3,333,333,

吉姆
精细 & 44 & 4,444,444

莎拉
谢谢 & 55 & 5,555,555
& 66 & 6,666,666

怎么样?

I insert several(array) value with json_encode in one row from database table, now want echo they as order with jquery.

This is output from my PHP code:

[{
    "guide": null,
    "residence": [{
        "name_r": "jack"
    }, {
        "name_r": "jim"
    }, {
        "name_r": "sara"
    }],
    "residence_u": [{
        "units": ["hello", "how", "what"],
        "extra": ["11", "22", "33"],
        "price": ["1,111,111", "2,222,222", "3,333,333"]
    }, {
        "units": ["fine"],
        "extra": ["44"],
        "price": ["4,444,444"]
    }, {
        "units": ["thanks", "good"],
        "extra": ["55", "66"],
        "price": ["5,555,555", "6,666,666"]
    }]
}]

I want as(output):

jack
hello & 11 & 1,111,111
how & 22 & 2,222,222
what & 33
& 3,333,333,

jim
fine & 44 & 4,444,444

sara
thanks & 55 & 5,555,555
good & 66 & 6,666,666

How is it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

入怼 2024-12-13 11:28:11

假设

  • 您已经知道如何发出从 PHP 脚本中获取此信息的 ajax 请求,
  • 那么顶级数组中始终有一个元素(如果没有,您可以再添加一层迭代),
  • 通过输出您的意思是访问相应的元素 -我已调用 console.log 但您可以 alert() 或放入 DOM 元素或任何

您可以在 jQuery 中执行类似操作的内容(这里是 <一个href="http://jsfiddle.net/nogoodatcoding/erq3d/" rel="nofollow">没有网络部分的小提琴示例 您将在控制台中看到输出)

var data = response[0]; //response is the data received by the jQuery ajax success callback
var residences = data.residence;
var residence_u = data.residence_u;

$.each(residences, function(index, val){
    var name = val.name_r;
    console.log(name);

    var info = residence_u[index]; //get the corresponding residence_u element

    $.each(info.units, function(index, val){
        var unit = val;
        var extra = info.extra[index];
        var price = info.price[index];
        console.log( val + " & " + extra + " & " + price);
    });
});

Assuming

  • you already know how make the ajax request that fetches this information from your PHP script
  • there is always one element in the top-level array (if not, you could add one more level of iteration)
  • by output you mean access the corresponding elements - I've put in calls to console.log but you could alert() or put into a DOM element or whatever

you could do something like this in jQuery (here's an example fiddle without the network part. You'll see the output in your console)

var data = response[0]; //response is the data received by the jQuery ajax success callback
var residences = data.residence;
var residence_u = data.residence_u;

$.each(residences, function(index, val){
    var name = val.name_r;
    console.log(name);

    var info = residence_u[index]; //get the corresponding residence_u element

    $.each(info.units, function(index, val){
        var unit = val;
        var extra = info.extra[index];
        var price = info.price[index];
        console.log( val + " & " + extra + " & " + price);
    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文