Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/421.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带有Javascript变量的JSON_Javascript_Json_Variables - Fatal编程技术网

带有Javascript变量的JSON

带有Javascript变量的JSON,javascript,json,variables,Javascript,Json,Variables,我想将varialbe名称解析为JSON字符串: JSON: systemvar.continue是在Javascript中定义的 如何编写JSON代码来使用Javascript变量 有什么想法吗 Best KurtJSON是JavaScript语法的一个安全、受限的子集。如果要允许任何JavaScript语法,包括可能不安全的语法,请使用而不是JSON.parse()如果在JavaScript中定义了systemvar.continue,请按如下方式使JSON有效: { "items"

我想将varialbe名称解析为JSON字符串:

JSON:

systemvar.continue是在Javascript中定义的

如何编写JSON代码来使用Javascript变量

有什么想法吗


Best Kurt

JSON是JavaScript语法的一个安全、受限的子集。如果要允许任何JavaScript语法,包括可能不安全的语法,请使用而不是
JSON.parse()

如果在JavaScript中定义了
systemvar.continue
,请按如下方式使JSON有效:

{
    "items": [
        {
            "id": "2000",
            "buttons": [{
                "text": "continue", // <-- only pass the property name
            }]
        }
}
当然,这是硬编码的,并且假设您解析的JSON在名为
JSON
的变量中引用。我假设您正在遍历该结构,并且在某个时候会遇到
文本:“continue”
。当您到达那里时,将其作为属性名插入

// in traversal
systemvar[ val.text ];


我还建议您避免使用单词
continue
,因为它是一个保留字,尽管它可以与方括号中的字符串一起使用。

我认为您可以使用的是

客户端将具有以下html代码:

<script>
function readData(json_arr) {
    //`json_arr` will be the javascript array.
    //do whatever you want to do with the array here.
}
</script>
<script src="your_server.example.com/getData?jsonp=readData"></script>
请注意,这意味着您返回的“JSON数组”由浏览器的javascript引擎自动执行

// in traversal
systemvar[ val.text ];
<script>
function readData(json_arr) {
    //`json_arr` will be the javascript array.
    //do whatever you want to do with the array here.
}
</script>
<script src="your_server.example.com/getData?jsonp=readData"></script>
readData(
{
    "items": [
        {
            "id": "2000",
            "buttons": [{
                "text": systemvar.continue,
            }]
        }
    }
)