简体   繁体   English

如何使用带有随机键的json2html

[英]How to use json2html with random keys

So, I clearly understand how to transform JSON to HTML if I know what type of data i will get in json. 因此,如果我知道我将在json中获取哪种类型的数据,我将清楚地理解如何将JSON转换为HTML。

But how can I use json2html if I don't know what kind of keys I will get from server? 但是,如果我不知道从服务器获得哪种密钥,该如何使用json2html?

Here code, that works correctly with static keys: 这里的代码,可以正确地使用静态键:

 var data = {'json': [{ 'order': 'By', 'name': 'Stack', 'randomkey': '3', 'randomkey_n': '0', 'score': '121', 'id': '540' }]}; var transform = { tag: 'tr', children: [{ "tag": "td", "html": "${order}" }, { "tag": "td", "html": "${name}" }, { "tag": "td", "html": "${randomkey}" }, { "tag": "td", "html": "${randomkey_n}" }, { "tag": "td", "html": "${score}" }] }; $('#placar > tbody ').json2html(data.json, transform); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="http://json2html.com/js/json2html.js"></script> <script src="http://json2html.com/js/jquery.json2html.js"></script> <div class="container"> <p> <table id="placar" class="table table-condensed table-bordered"> <thead> <tr> <th>1</th> <th>2</th> <th>3</th> <th>4</th> <th>5</th> </tr> </thead> <tbody></tbody> </table> </div> 

I understand, that I should use inline function in transform , but I can't understand how to return value of "random" keys. 我知道,我应该在transform使用内联函数,但是我不明白如何返回“随机”键的值。

You can list the properties with Object.keys , exclude the id property (as you seem not to render it) with .filter() and finally .map() those to the object structure you need for the children property: 您可以使用Object.keys列出属性,使用.filter()排除id属性(似乎没有渲染它),最后将.map()排除到children属性所需的对象结构中:

 var data = {'json': [{ 'order': 'By', 'name': 'Stack', 'randomkey': '3', 'randomkey_n': '0', 'score': '121', 'id': '540' }]}; var transform = { tag: 'tr', children: Object.keys(data.json[0]) // get keys .filter(key => key !== 'id') // exclude id .map(key => ({ // convert to object for transformation "tag": "td", "html": "${" + key + "}" })) }; $('#placar > tbody ').json2html(data.json, transform); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="http://json2html.com/js/json2html.js"></script> <script src="http://json2html.com/js/jquery.json2html.js"></script> <div class="container"> <p> <table id="placar" class="table table-condensed table-bordered"> <thead> <tr> <th>1</th> <th>2</th> <th>3</th> <th>4</th> <th>5</th> </tr> </thead> <tbody></tbody> </table> </div> 

Filtering out more than one key 过滤出多个键

In case you have other properties that should be excluded like id , then you could do this: 如果您还有其他应排除的属性(例如id) ,则可以执行以下操作:

.filter(key => !['id', 'otherfield', 'comment'].includes(key))

Or, if you have many such properties, improve efficiency by first defining a set once: 或者,如果您有许多这样的属性,请先定义一次集合来提高效率:

const excludes = new Set(['id', 'otherfield', 'comment', /* many others...*/]);

...and then the filtering becomes: ...然后过滤变为:

.filter(key => !excludes.has(key))

Nice answer tincot .. just to add another way of doing this that might be a little easier to understand. 很好的答案tincot ..只是添加了另一种可能更容易理解的方法。 You could transform the json object into an array with the property name and value then use a static transform 您可以将json对象转换为具有属性名称和值的数组,然后使用静态转换

var data = {'json': [{
    'order': 'By',
        'name': 'Stack',
        'randomkey': '3',
        'randomkey_n': '0',
        'score': '121',
        'id': '540'
}]};

var transform = {"<>": "tr","html":[
            {"<>":"td","html":"${val}"}
        ]};

var _data = [];

for(var i=0; i < data.json.length; i++){

  var out = [];

  for(var prop in data.json[i]) 
    if(prop !== "id") out[i] = {"name":prop,"val":data.json[i][prop]};

  _data.push(out);
}

$('#placar > tbody ').json2html(_data, transform);

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

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