如何使用Th:每个
这是来自Spring MVC控制器的JsonObject列表。
List<JSONObject> jsonDataList =
[{"key1":"value1","key2":"value2","key3":"value3","key4":"value4"}, {"key1":"value1","key2":"value2","key3":"value3","key4":"value4"}]
如何使用每个:每个:迭代胸腺中的jsonobject列表?
下面的HTML文件中的代码:=&gt;
<tr th:each="data: ${jsonDataList}">
<td align="center"><span th:text="${data.key1}"></span></td> // getting exception here
</tr>
得到例外为: 引起:org.attoparser.parseexception:评估弹簧表达式的异常:“ data.key1”
Here is that list of JSONObject which is coming from Spring MVC Controller.
List<JSONObject> jsonDataList =
[{"key1":"value1","key2":"value2","key3":"value3","key4":"value4"}, {"key1":"value1","key2":"value2","key3":"value3","key4":"value4"}]
How to Iterate List of JSONObject in Thymeleaf using th:each?
Code IN HTML FILE below:=>
<tr th:each="data: ${jsonDataList}">
<td align="center"><span th:text="${data.key1}"></span></td> // getting exception here
</tr>
Getting Exception as :
Caused by: org.attoparser.ParseException: Exception evaluating SpringEL expression: "data.key1"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一种方法,但它做出了一些假设:
a)每个JSON对象具有相同数量的条目(否则您可以有一个破烂的表,每行中包含不同数量的单元格)。
b)每个JSON对象都以相同的顺序具有相同的键(如果您希望表具有一致的列标题)。
同样,问题中的示例JSON假定所有值都是字符串(
value1
等)。如果您的JSON值中有不同类型的对象,则需要确保它们具有所需的字符串表示(例如使用toString()
)。方法:
第一个
&lt; tr&gt;
部分负责从列表中的第一个对象读取JSON对象键:最终
&lt; tr&gt;
e节是相似的,但使用查找其相关值的关键:由此产生的HTML为您提供了一个简单的表:
参考:
th:block
标签记录在在这里。可用于
jsonobject
的方法已记录在这里。Here is one approach, but it makes some assumptions:
a) Each JSON object has the same number of entries (otherwise you could have a ragged table, containing different numbers of cells in each row).
b) Each JSON object has the same keys in the same order (if you want the table to have consistent column headings).
Also, the sample JSON in the question assumes all values are strings (
value1
and so on). If you have different types of objects in your JSON values, then you will need to ensure they have the required string representations (e.g. usingtoString()
).The approach:
The first
<tr>
section handles reading the JSON object keys from the first object in the list:The final
<tr>
section is similar, but uses the keys to look up their related values:The resulting HTML gives you a simple table:
References:
The
th:block
tag is documented here.The methods available to be used for
JSONObject
are documented here.怎么样?
how about this?