gpt4 book ai didi

javascript - 如何将数据对象添加到 JSON 对象?

转载 作者:行者123 更新时间:2023-11-28 07:37:45 25 4
gpt4 key购买 nike

我想将列表 2 的数据 [] 添加到列表 1 作为子项。所需的输出是列表 3。但是通过我的代码,我得到空字符串作为子项的值。 _getJsonObject(id,user) 中的 api 调用带来列表 2。

我的代码如下:

var strList = function (){
var listItems= "";
$.ajax({
url: 'http://localhost:8088/api/v1/ui/performance/custom_link_groups?',
dataType: 'json',
async: false,
success: function(data){
for (var i = 0; i < data.data.length; i++){
data.data[i].children = _getJsonObject(data.data[i].$ID,data.data[i].$userId);
}
listItems = data;
}
});
return listItems;
}();


function _getJsonObject(id,user){
var rdata = "";
$.ajax({
url: 'http://localhost:8088/api/v1/ui/performance/custom_links?group='+id+'&user='+user,
dataType: 'json',
async: false,
success: function(data){
rdata = data.data;
}
});
return rdata;
}

列表 1

{
"response_code": 200,
"error_code": "",
"error_message": "",
"status": "SUCCESS",
"row_count": 5,
"data": [
{
"ID": "84",
"groupLabel": "Interface Util",
"isPrivate": "0",
"userId": "user1"
},
{
"ID": "85",
"groupLabel": "Hendrick Test Mweb",
"isPrivate": "0",
"userId": "user2"
},
{
"ID": "86",
"groupLabel": "test",
"isPrivate": "0",
"userId": "user4"
},
{
"ID": "87",
"groupLabel": "Interface Util",
"isPrivate": null,
"userId": "user3"
},
{
"ID": "88",
"groupLabel": "testing",
"isPrivate": "0",
"userId": "user5"
}
],
"meta": ""

}

列表 2

{
"response_code": 200,
"error_code": "",
"error_message": "",
"status": "SUCCESS",
"row_count": 2,
"data": [
{
"ID": "53",
"attributeId": "2194242",
"groupId": "85",
"isSummary": null,
"label": "DEV-SUPPORT-PC, DEV-SUPPORT-PC : Uptime",
"rrdGraphGroup": null
},
{
"ID": "54",
"attributeId": "2255447||2255453",
"groupId": "85",
"isSummary": null,
"label": "DEV-SUPPORT-PC, Intel(R)%2082578DC%20Gigabit%20Network%20Connection%20 : Utilization",
"rrdGraphGroup": "Utilization"
}
],
"meta": ""

}

列表 3

{
"response_code": 200,
"error_code": "",
"error_message": "",
"status": "SUCCESS",
"row_count": 5,
"data": [
{
"ID": "84",
"groupLabel": "Interface Util",
"isPrivate": "0",
"userId": "user1",
"children": ""
},
{
"ID": "85",
"groupLabel": "Hendrick Test Mweb",
"isPrivate": "0",
"userId": "user2",
"chlidren": [
{
"ID": "53",
"attributeId": "2194242",
"groupId": "85",
"isSummary": null,
"label": "DEV-SUPPORT-PC, DEV-SUPPORT-PC : Uptime",
"rrdGraphGroup": null
},
{
"ID": "54",
"attributeId": "2255447||2255453",
"groupId": "85",
"isSummary": null,
"label": "DEV-SUPPORT-PC, Intel(R)%2082578DC%20Gigabit%20Network%20Connection%20 : Utilization",
"rrdGraphGroup": "Utilization"
}
]
},
{
"ID": "86",
"groupLabel": "test",
"isPrivate": "0",
"userId": "user4",
"children": ""
},
{
"ID": "87",
"groupLabel": "Interface Util",
"isPrivate": null,
"userId": "user3",
"children": ""
},
{
"ID": "88",
"groupLabel": "testing",
"isPrivate": "0",
"userId": "user5",
"children": ""
}
],
"meta": ""
}

最佳答案

var strList = function (callback){
var listItems= "";
$.ajax({
url: 'http://localhost:8088/api/v1/ui/performance/custom_link_groups?',
dataType: 'json',
async: false,
success: function(data){
for (var i = 0; i < data.data.length; i++){
_getJsonObject(data.data[i].$ID,data.data[i].$userId,

function(data)
{
data.data[i].children = data;
});
}
callback(data);
}
});
return listItems;
}();


function _getJsonObject(id,user, callback){
var rdata = "";
$.ajax({
url: 'http://localhost:8088/api/v1/ui/performance/custom_links?group='+id+'&user='+user,
dataType: 'json',
async: false,
success: function(data){
callback(data.data);
}
});
return rdata;
}

关于javascript - 如何将数据对象添加到 JSON 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28406536/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com