简体   繁体   English

使用JSON和JQuery解析变量中的数据

[英]Parcing data from a variable using JSON and JQuery

I'm trying to request the value of a variable using JSON, but something is wrong with my JSON code and I don't know what it is. 我正在尝试使用JSON请求变量的值,但JSON代码出了点问题,我不知道它是什么。 Can anybody give me some tip? 有人可以给我一些小费吗?

JSON code: JSON代码:

{
"counter1": {
        "variable": :="webdata".counter1:
        },
"counter2": {
        "variable": :="webdata".counter2:
        },
}

JQuery code: jQuery代码:

$(document).ready(function(){
    $.ajaxSetup({ cache: false }); 
setInterval(function() {
    $.getJSON("json_data.json", function(result){
        $('#counter1').text((result.variable[0]).trim());
    $.getJSON("json_data.json", function(result){
        $('#counter1').text((result.variable[0]).trim());
    });
},1000);
});

HTML code: HTML代码:

<p label="counter1">0</p>
<p label="counter2">0</p>

Thanks in advance! 提前致谢!

Your JSON isn't the only thing that is wrong. JSON并不是唯一出问题的地方。 Why are you calling the same code twice in Javascript (what you call jQuery) 为什么在Javascript中两次调用相同的代码(称为jQuery)

And the "label" attribute in your "p" element is wrong it should be "id". 并且“ p”元素中的“标签”属性是错误的,应该为“ id”。

I'm not sure what is generating that JSON but this is a possible way of achieving what you are going for. 我不确定是什么生成了JSON,但这是实现目标的一种可能方法。

change the JSON output to: 将JSON输出更改为:

{
"counters": [
    {
        "variable" : "COUNTER 1_VARIABLE"
    },
    {
        "variable" : "COUNTER2_VARIABLE"
    }
]
}

Where it is in all caps should the content of your variable because this is the serialized output 大写字母应位于变量的大写位置,因为这是序列化的输出

JQuery code: jQuery代码:

$(document).ready(function(){
$.ajaxSetup({ cache: false }); 
setInterval(function() {
$.getJSON("json_data.json", function(result){
$('#counter1').text((result.counters[0].variable).trim());
$.getJSON("json_data.json", function(result){
    $('#counter2').text((result.counters[1].variable).trim());
});
},1000);
});

It is worth noting that the use of "$.ajaxSetup()" is discouraged in the docs. 值得注意的是,文档中不鼓励使用“ $ .ajaxSetup()”。 https://api.jquery.com/jquery.ajaxsetup/ https://api.jquery.com/jquery.ajaxsetup/

HTML code: HTML代码:

<p id="counter1">0</p>
<p id="counter2">0</p>

First of all thanks to all of you for your prompt answer. 首先,感谢大家的迅速回答。

I managed to get the JSON work as follows:(eventhough I still have a problem) 我设法使JSON工作如下:(尽管我仍然有问题)

JSON code (json/json_data.json): JSON代码(json / json_data.json):

{
    "counters": [
        {
            "variable": ":='webdata'.counter1:"
        },
        {
            "variable": ":='webdata'.counter2:"
        }
    ]
}

JQuery code (my_script.js): 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 code (index.htm): HTML代码(index.htm):

<tr>
    <td>PT000</td>
    <td>Process value 0</td>
    <td> <label id="counter0">0</label> </td>
    <td>bar</td>
</tr>

Now the point is that I'm getting the text written in the JSON printed on the screen, this is the name of my variable, we can say is the path to reach the value I want to print. 现在,关键是要在屏幕上打印以JSON格式编写的文本,这是我的变量的名称,可以说是达到我要打印的值的路径。

The point is that if I use an HTM file and write on it the name of the variable and then I simply use JQuery to get the data it works, but I have plenty of values I want to print out, so I thought that JSON could help me to manage this issue. 关键是,如果我使用HTM文件并在其上写入变量的名称,然后我仅使用JQuery来获取其有效的数据,但是我想打印出很多值,因此我认为JSON可以帮助我解决这个问题。 Here the example with the HTM file: 这里是带有HTM文件的示例:

HTM code (IOValue1.htm): HTM代码(IOValue1.htm):

:="webdata".counter1:

JQuery code (my_script.js): jQuery代码(my_script.js):

$(document).ready(function(){
    $.ajaxSetup({ cache: false }); 
setInterval(function() {
    $.get("IOValue1.htm", function(result){
        $('#counter1').text((result));
    });
},1000);
});

HTML code (index.htm): HTML代码(index.htm):

<tr>
    <td>PT001</td>
    <td>Process value 1</td>
    <td> <label id="counter1">0</label> </td>
    <td>bar</td>
</tr>

Probably I'm not considering some fundamental fact from JSON, but I'm not too experienced. 可能我没有考虑JSON的一些基本事实,但是我不太有经验。 Any idea? 任何想法?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM