我一直在努力在我正在开发的项目中实现json2html,我遇到了一个小问题。基本上,我的转换运行但它不包括应该的JSON对象。
我设置了一些东西,以便调用MongoDB,并以字符串形式返回JSON数据。我将该字符串传递给“var data”,然后使用我的转换代码将其传递给json2html.transform(data,transform)。
以下是我的转换代码示例:
transform = [
{"tag":"html","children":[
{"tag":"body","children":[
{"tag":"p","html":"First Name: ${firstName}"},
{"tag":"p","html":"Last Name: ${lastName}"},
]}
]}
];
...以及来自MongoDB的相应JSON数据......
{biographicData: {firstName:'约翰', lastName:'Doe', birthDate:'10 / 15/1983', 电子邮件:'johndoe@gmail.com', workPhone:'678-901-2345', mobilePhone:'098-765-4321', homePhone:'123-456-7890'} }
...但我的HTML看起来像这样:
名字:
姓氏:
无处可寻找JSON对象。知道这可能是什么问题吗?
答案 0 :(得分:0)
Tom说得对.. $ {biographicData.whatever}应该已经解决了这个问题。试试这个可以工作的代码spinet
var data = { biographicData: { firstName: 'John', lastName: 'Doe', birthDate: '10/15/1983', email: 'johndoe@gmail.com', workPhone: '678-901-2345', mobilePhone: '098-765-4321', homePhone: '123-456-7890' } };
var transform = {"tag":"html","children":[
{"tag":"body","children":[
{"tag":"p","html":"First Name: ${biographicData.firstName}"},
{"tag":"p","html":"Last Name: ${biographicData.lastName}"},
]}
]};
document.write(json2html.transform(data,transform));
我建议您删除html&变换中的body标签,而不是将它包含在html文件中。这样你就可以传递像这样的值数组
var data = [{ biographicData: { firstName: 'John', lastName: 'Doe', birthDate: '10/15/1983', email: 'johndoe@gmail.com', workPhone: '678-901-2345', mobilePhone: '098-765-4321', homePhone: '123-456-7890' } }];
var transform = [
{"tag":"p","html":"First Name: ${biographicData.firstName}"},
{"tag":"p","html":"Last Name: ${biographicData.lastName}"},
];
document.write(json2html.transform(data,transform));
否则你最终会得到多个html&生成的html中的头标记