jQuery和Json推送问题

时间:2013-06-06 22:17:55

标签: json

我可以使用push({})来创建我的javascript数组,但是我的一个字段有自己的列表。所以我无法在关卡上执行push()。

如下所示,定义了tradeDetails数组,EXTENSIONS字段有一组值。我如何处理下面这个。

function TradeDetailsToJson(xml) {    

var tradeDetails = [];
var tradeId = $(xml).find("tradeHeader tradeId").text();
var tradeDate = $(xml).find("tradeHeader tradeDate").text();
var tradeType = $(xml).find("tradeHeader tradeType").text();
var counterparty = $(xml).find("tradeHeader counterparty").text();
var internalUnit = $(xml).find("tradeHeader internalUnit").text();
var buySell = $(xml).find("tradeHeader buySell").text();
var status = $(xml).find("tradeHeader status").text();
tradeDetails.push({
    "tradeId": tradeId,
    "tradeDate": tradeDate,
    "tradeType": tradeType,
    "counterparty": counterparty,
    "internalUnit": internalUnit,
    "buySell": buySell,
    "status": status,
    "**extensions**":[]
});

// NOW I'D LIKE TO PUSH MY EXTENSIONS !!
var extName = ""
var extValue = "";
$(xml).find("extensions extension").each(function () {
    extName = $(this).find("name").text();
    extValue = $(this).find("value").text();
    //tradeDetails[extensions].push({ "name": name, "value": value }); ??????
});
}

先谢谢你。 鲍勃

1 个答案:

答案 0 :(得分:1)

再次使用推送extensions属性?

tradeDetails.push({
    "tradeId": tradeId,
    "tradeDate": tradeDate,
    "tradeType": tradeType,
    "counterparty": counterparty,
    "internalUnit": internalUnit,
    "buySell": buySell,
    "status": status,
    "extensions":[]
});

tradeDetails.extensions.push({ "name": name, "value": value });

根据您的尝试,您需要在引号中包装extensions这是对象tradeDetails的关键字。

所以改变

tradeDetails[extensions].push({ "name": name, "value": value });

tradeDetails["extensions"].push({ "name": name, "value": value });