在Json调用中使用变量值

时间:2018-04-16 09:10:04

标签: javascript json variables

这是我的json信息。

{"RAW":<br>
&nbsp;&nbsp;&nbsp;{"CAN":<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{"AUD":     {"FROMSYMBOL":"CAN","BUY":"0.317993","SELL":"0.31012","MARKETCAP":"12,787,119","CHANGE":"2.13%"}}

"RISE":
    {"AUD":
        {"FROMSYMBOL":"RISE","BUY":"0.337224","SELL":"0.331142","MARKETCAP":"13,396,190","CHANGE":"0.00%"}}

"SAFEX":
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{"AUD":
{"FROMSYMBOL":"SAFEX","BUY":"0.013389","SELL":"0.013389","MARKETCAP":"29,777,352","CHANGE":"0.00%"}}

"HST":
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{"AUD":{"FROMSYMBOL":"HST","BUY":"1.00726","SELL":"0.980698","MARKETCAP":"30,587,338","CHANGE":"9.66%"}}

"HAV":<br>&nbsp;&nbsp;{"AUD":
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{"FROMSYMBOL":"HAV","BUY":"0.601209","SELL":"0.495748","MARKETCAP":"32,375,759","CHANGE":"3.38%"}}

这是我的代码。

The CAN page = 

    document.getElementById("CAN-PRICE").innerHTML = jsonObj.RAW.CAN.AUD.BUY;

The RISE page =

    document.getElementById("RISE-PRICE").innerHTML = jsonObj.RAW.RISE.AUD.BUY;

我不想为每个页面创建特定页面。

我可以做的是拉一个特定的变量值,例如

 var foo = "SAFEX";

我想要做的是将这个值插入json调用中,就像这样。

 document.getElementById("foo-PRICE").innerHTML = jsonObj.RAW.foo.AUD.BUY;

我已经尝试了一切我能想到的eval()似乎什么都不做,无论我怎么把它砍掉。

任何建议都将不胜感激。

1 个答案:

答案 0 :(得分:0)

document.getElementById(foo +"-PRICE").innerHTML = jsonObj.RAW[foo].AUD.BUY;