3

在 AJAX 调用之后,我得到如下 JSON 数据:

{
  "detailPrice": {
    "server": {
      "amount": "8",
      "usedPrice": "10",
      "discountPrice": "-1",
      "totalPrice": "9"
    },
    "network": {
      "amount": "8",
      "usedPrice": "10",
      "discountPrice": "-1",
      "totalPrice": "9"
    },
    "storage": {
      "amount": "8",
      "usedPrice": "10",
      "discountPrice": "-1",
      "totalPrice": "9"
    },
    "loadBalancer": {
      "amount": "8",
      "usedPrice": "10",
      "discountPrice": "-1",
      "totalPrice": "9"
    },
    "others": {
      "amount": "8",
      "usedPrice": "10",
      "discountPrice": "-1",
      "totalPrice": "9"
    },
    "support": {
      "amount": "8",
      "usedPrice": "10",
      "discountPrice": "-1",
      "totalPrice": "9"
    },
    "totalPrice": {
      "totalUsedPrice": "8",
      "totalDiscountPrice": "-2",
      "missedPrice": "10",
      "tax": "9",
      "otherDiscount": "-1"
    }
  }
}

我必须将这些数据附加到视图中,所以我编写了这样的代码:

var serverpriceHTML = "총 " + result.detailPrice.server.amount + "대<br/>";
serverpriceHTML += "이용요금 " + result.detailPrice.server.usedPrice + "원&lt;br/>";
serverpriceHTML += "할인요금 " + result.detailPrice.server.discountPrice + "원&lt;br/>";
var serverTotalPriceHTML = result.detailPrice.server.totalPrice + "원";
$("#server_price").html(serverpriceHTML);
$("#server_totalprice").html(serverTotalPriceHTML);

var networkpriceHTML = "총 " + result.detailPrice.network.amount + "대<br/>";
networkpriceHTML += "이용요금 " + result.detailPrice.network.usedPrice + "원&lt;br/>";
networkpriceHTML += "할인요금 " + result.detailPrice.network.discountPrice + "원&lt;br/>";
var networkTotalPriceHTML = result.detailPrice.network.totalPrice + "원";
$("#network_price").html(networkpriceHTML);
$("#network_totalprice").html(networkTotalPriceHTML);

正如你所看到的,它有很多重复的代码,所以我试图为它创建一个私有函数。

问题是:

result.detailPrice.server.amount

我必须更改servernetworkstorageloadbalancer才能获取数据,但我不确定如何更改。

如果我像这样:

function makeHTML(price, totalPrice, name) {
  var test = "result.detailPrice" + name + ".amount";
  var serverpriceHTML = "총 " + test + "대<br/>";
  serverpriceHTML += "이용요금 " + result.detailPrice.server.usedPrice + "원&lt;br/>";
  serverpriceHTML += "할인요금 " + result.detailPrice.server.discountPrice + "원&lt;br/>";  
  var serverTotalPriceHTML = result.detailPrice.server.totalPrice + "원";

  $(price).html(serverpriceHTML);
  $(totalPrice).html(serverTotalPriceHTML);
}

这只是添加字符串测试。有什么好主意吗?

4

2 回答 2

3

[]当您想使用字符串值时,使用符号来访问值。

代替

var test = "result.detailPrice" + name + ".amount";

尝试

var test = result.detailPrice[name].amount;

如果要替换变量值,则点表示法不起作用。

简单示例

var obj = {
       "ball" : "bat"
    };

var name = "ball";

// 如果你使用点符号

obj.name (Gives you undefined) (它将尝试获取 get key = name)它不会替换变量名

// [] 括号表示法

obj[name] (Gives you bat) 将替换变量名并获取值

于 2013-08-07T06:13:09.240 回答
0

您可以指定不同的变量值,例如:-

//This will give you some private variables for following:-
var constants={
server=function(){return 0},
network=function(){return 1},
storage=function(){return 2},
loadBalancer=function(){return 3},
others=function(){return 4},
suport=function(){return 5},
totalPrice=function(){return 6}
}

然后您可以在如下变量中获取 JSON:-

您可以按如下方式更改 JSON:-

var jsonData= {
           "detailPrice":{
              {
                 "amount":"8",
                 "usedPrice":"10",
                 "discountPrice":"-1",
                 "totalPrice":"9"
              },
                {
                 "amount":"8",
                 "usedPrice":"10",
                 "discountPrice":"-1",
                 "totalPrice":"9"
              },
                {
                 "amount":"8",
                 "usedPrice":"10",
                 "discountPrice":"-1",
                 "totalPrice":"9"
              },
                {
                 "amount":"8",
                 "usedPrice":"10",
                 "discountPrice":"-1",
                 "totalPrice":"9"
              },
                {
                 "amount":"8",
                 "usedPrice":"10",
                 "discountPrice":"-1",
                 "totalPrice":"9"
              },
                {
                 "amount":"8",
                 "usedPrice":"10",
                 "discountPrice":"-1",
                 "totalPrice":"9"
              },
                {
                 "totalUsedPrice":"8",
                 "totalDiscountPrice":"-2",
                 "missedPrice":"10",
                 "tax":"9",
                "otherDiscount": "-1"
              }

           }
        }

然后在任何地方调用它,如下所示: -

jsonData.detailPrice[constant.server];//For Server Details
jsonData.detailPrice[constant.network];//For Network Details

等等。希望这听起来有帮助。

于 2013-08-07T06:28:04.313 回答