在没有JSON解析的情况下在Jade中输出服务器生成的JSON对象 [英] Output a server generated json object in jade without json parse
问题描述
我有一个不符合JSON标准的JSON对象,并且我无法更改该对象的结构以使其符合JSON标准.
I have a JSON object that does not conform to JSON standards, and I cannot change the structure of the object to make it adhere to JSON standards.
我需要将此对象呈现在Jade模板的javascript块中间.该对象实际上是在模板的功能块中使用的配置对象.
I need to make this object render in the middle of a javascript block in a Jade template. The object is actually a configuration object that is going in a function block in the template.
这里是对象.
{
services: [],
version: "1438276796258",
country: "default",
role: "User",
Zack_Init: function () {
},
Zack_Global: function (event) {
},
Zack_PostRender: function () {
},
renderers: ['Renderer', 'NONE']
}
更新 这是我从JS文件中获取该对象的方式.
UPDATE Here is how I am getting that object from a JS file.
function readJSFile(url, filename, callback) {
fs.readFile(url, "utf-8", function (err, data) {
if (err) {
callback(err);
return;
}
try {
callback(filename, data);
} catch (exception) {
callback(exception);
}
});
}
当JSON.stringify处理对象时,它将在转换过程中删除这三个函数.
When JSON.stringify processes the object it drops the three functions in the conversion process.
我正在添加一个插件以显示当前解决方案的进度.输出以下内容.剩下的唯一事情就是删除格式字符.
I am adding a plunker to show the progress of the current solution. Which outputs the below. The only thing left is to remove the formatting characters.
{"services":[],"version":"1438276796258","country":"default","role":"User","Zack_Init":function () {\n\n },"Zack_Global":function (event) {\n\n },"Zack_PostRender":function () {\n\n },"renderers":["Renderer","NONE"]}
function convertToString(obj) {
return JSON.stringify(obj, function(k, v) {
return (typeof v === 'function' ? ['@@beginFunction@@', v.toString(), '@@endFunction@@'].join('') : v);
}).replace(/"@@beginFunction@@|@@endFunction@@"/g, '');
}
obj = {
services: [],
version: "1438276796258",
country: "default",
role: "User",
Zack_Init: function() {
},
Zack_Global: function(event) {
},
Zack_PostRender: function() {
},
renderers: ['Renderer', 'NONE']
};
$('#test').text(convertToString(obj));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="test"></div>
推荐答案
为了安全地删除换行符,应先删除'comments':
In order to remove new line characters safely, 'comments' should be removed first:
function convertToString(obj) {
return JSON.stringify(obj, function(k, v) {
return (
typeof v !== 'function' ?
v :
v.toString().replace(/\/\/.*?$/mg, '') // removes single line comments
.replace(/\/\*[\s\S]*?\*\//g, '') // removes multi-line comments
.replace(/[\r\n]/g, '') // removes new line
);
}, 2).replace(/"(function.+)",?$/gm, '$1'); // removes quotes around functions
}
obj = {
services: [],
version: "1438276796258",
country: "default",
role: "User",
Zack_Init: function() {
// comment
var a = 1;// comment //
if(a === /*comment */ 3) {//comment
/* comment
comment*/
}
/*end*/
},
Zack_Global: function(event) {
},
Zack_PostRender: function() {
},
renderers: ['Renderer', 'NONE']
};
$('#result').text(convertToString(obj));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<pre id="result"></pre>
这篇关于在没有JSON解析的情况下在Jade中输出服务器生成的JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!