[英]Convert a text from JavaScript into HTML
我正在嘗試從JSON文件中獲取文本並將此文本顯示為HTML對象。 但是我改為將其讀取為字符串。
我有一個JSON文件,如下所示:
JSON代碼(json / json_data.json):
{
"counters": [
{
"variable": ":='webdata'.counter1:"
},
{
"variable": ":='webdata'.counter2:"
}
]
}
jQuery代碼(my_script.js):
$(document).ready(function(){
$.ajaxSetup({ cache: false });
setInterval(function() {
$.getJSON("json/json_data.json", function(result){
$('#counter0').text(result.counters[0].variable);
});
},1000);
});
HTML代碼(index.htm):
<tr>
<td>PT000</td>
<td>Process value 0</td>
<td> <label id="counter0">0</label> </td>
<td>bar</td>
</tr>
現在,關鍵是要在屏幕上打印以JSON格式編寫的文本,這是我的變量的名稱,可以說是達到我要打印的值的路徑。
另一方面,如果我使用HTML文件並在其上寫入變量的名稱,然后我僅使用jQuery來獲取有效的數據,但是我有很多要打印的值,所以我認為JSON可以幫助我解決這個問題。 以下是帶有HTML文件的示例:
HTML代碼(IOValue1.htm):
:="webdata".counter1:
jQuery代碼(my_script.js):
$(document).ready(function(){
$.ajaxSetup({ cache: false });
setInterval(function() {
$.get("IOValue1.htm", function(result){
$('#counter1').text((result));
});
},1000);
});
HTML代碼(index.htm):
<tr>
<td>PT001</td>
<td>Process value 1</td>
<td> <label id="counter1">0</label> </td>
<td>bar</td>
</tr>
可能我沒有考慮JavaScript如何處理從JSON文件獲得的字符串。 有人有主意嗎?
更改
$('#counter0').text(result.counters[0].variable);
至
var tmpArray = result["counters"];
var counterObject = tmpArray[0];
var variableValue = counterObject["variable"];
$('#counter0').text(variableValue);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.