简体   繁体   English

使用 handlebars.js 显示 JSON 数据

[英]Display JSON data using handlebars.js

I need to figure out how to print the full JSON response onto my page (or even parts of it), but I can't seem to figure it out.我需要弄清楚如何将完整的 JSON 响应打印到我的页面(甚至它的一部分)上,但我似乎无法弄清楚。 I am going to eventually fill out the page with more context later.稍后我将最终填写具有更多上下文的页面。

JS file: JS文件:

app.get('/', function(req, res) {
var context

apiLib.fetch('acct:chars', function(err, result){
    if(err) throw err

    context = result;
    console.log(result); 
    res.render('/', context);

    });

});

Handlebars:车把:

 {{#each context.characters}}
 {{this.name}} 
 {{/each}}

JSON Data: JSON 数据:

{
  "result": {
    '1234':{               //this is the character ID
      "characters": {
        "name": 'Daniel',
        "CharacterID": '1234'
        etc...
    }
   }
 }

My page prints nothing, but console logs everything.我的页面不打印任何内容,但控制台会记录所有内容。 I am using express.js to handle the routing.我正在使用 express.js 来处理路由。

If you want to display the stringified JSON, you can use an helper如果要显示字符串化的 JSON,可以使用助手

JS JS

Handlebars.registerHelper('toJSON', function(obj) {
    return JSON.stringify(obj, null, 3);
});

HTML <pre>{{toJSON result}}</pre> HTML <pre>{{toJSON result}}</pre>

Since you are iterating over objects in handlebars, it should be done as -由于您正在遍历车把中的对象,因此应该这样做 -

  {{#each context.characters}}
      {{#each this}}
         {{@key}} - {{this}}
      {{/each}}
  {{/each}}

One problem is the each helper, here: {{#each context.characters}} .一个问题是each助手,这里是: {{#each context.characters}} What each does is look within the current context for the variable. each所做的是在当前上下文中查找变量。 You've already passed Handlebars the context object when you called res.render('/', context) .当您调用res.render('/', context)时,您已经将context对象传递给 Handlebars 。 So now Handlebars is looking within the context object for an attribute called context , and it won't find it.所以,现在把手正在寻找的context的称为属性的对象context ,也不会发现它。 So you should change that line of code to {{#each characters}} .因此,您应该将该行代码更改为{{#each characters}}

The same thing applies to where you wrote {{this.name}} .同样的事情适用于你写{{this.name}} I think that will actually work if you fix the other problem, but the this is unnecessary, because Handlebars looks at this (the current context) automatically.我认为如果您解决其他问题, this实际上会起作用,但这是不必要的,因为 Handlebars 会自动查看this (当前上下文)。 So just {{name}} should be fine there.所以只有{{name}}应该没问题。

Finally, if you're really trying to just display the JSON file as a page of plain text, you don't need to run it through a Handlebars template.最后,如果您真的只是想将 JSON 文件显示为纯文本页面,则不需要通过 Handlebars 模板运行它。 You can just use res.json(context) .你可以只使用res.json(context) This will return a JSON response.这将返回一个 JSON 响应。 If your server is configured to handle the application/json MIME type, it should render as plain text in the browser.如果您的服务器配置为处理application/json MIME 类型,它应该在浏览器中呈现为纯文本。

If you are using it with Node.js and mongoDB this answer is for you;如果您将它与 Node.js 和 mongoDB 一起使用,则此答案适合您;

Let's say you have the following response from your database called data :假设您从名为data的数据库中得到以下响应:

{                                
  _id: "5AAAAAAAAAe04fc1a1", //some id idk
  BUSINESS: 'FLEX TAPE',          
  SHOWMAN: 'PHIL SWIFT',            
  CONTACT: 'PHIL SWIFT',         
  PHONE: '11111111113',       
  ETC: 'yes',                          
}    

What you might want to look for to display the fields is to take advantage from client-side rendering.您可能希望寻找显示字段的方法是利用客户端渲染。 piping the data with the classic JSON.stringify(data) as okData for instance.. then you can just reference it using the {{}} notation.例如,使用经典的JSON.stringify(data)作为okData管道data ..然后您可以使用{{}}表示法引用它。

e:g.例如。

<p id="some-id" style="display: none"> {{okData}} </p>

<script>
let jsonStrLookup = document.getElementById('some-id').innerHTML
let js = JSON.parse(jsonStrLookup)

console.log(js)
</script>

This would work for SIMPLE applications hope it helps out, and if something seems off comment, I always check on here, and will try to help you.这适用于简单的应用程序,希望它有所帮助,如果有什么东西看起来不对劲,我总是在这里检查,并会尽力帮助你。

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

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