将jsonencode php转换为javascript,但不要使用双引号

convert json_encode php to javascript but without double quotes

本文关键字:php jsonencode 转换 javascript      更新时间:2023-09-26

这里是php json_encode()调用的输出:

//start
//docStructure version 1.0.alpha compiled September 16th, 2014 at 9:18PM
var docStructure={"updateTime":"2014-09-16 21:18:19","listeners":[{"name":"eFaxListener","active":"0","url":"?action=faxvcheckinboundfax"}],"sections":{"defaultLayout":{"header":[]}},"editors":{"sideeditor":{"state":"closed","width":"0","context":""}}}//end

请注意,所有的键都有双引号。

为了便于阅读,我更希望钥匙不要有双引号。有什么不同吗?我更愿意识别为

alert(docStructure.editors.siteeditor.state);

与。

alert(docStructure['editors']['siteeditor']['state'])

或者我仍然可以将它们与键作为字符串互换使用吗?

谢谢,这是建立我对javascript思想的理解。。

JSON格式要求键是双引号的,一旦被javascript解析,它只是一个普通对象,所以您可以通过两种方式访问它们,假设您的键是有效的对象键(不能以数字或特殊字符开头)。

您正在寻找jQuery.parseJSON(json)

描述:

获取格式良好的JSON字符串,并返回生成的JavaScript对象