将字符串解析为 JSON [英] Parse string into JSON
问题描述
我有一个对象数组 $arr
并且一个对象有一个名为 jsonData
的属性,它在字符串中包含 json 数据,如何将该字符串解析为实际的 JSON对象和检索让我们说键 name
的值?我试过这个:
I have an array of objects $arr
and an object has a property named as jsonData
which contains json data in a string, how to parse that string to actual JSON object and retrieve lets say value for key name
? I tried this:
#foreach ($obj in $arr)
#set ($jsonData = "#evaluate(${obj.jsonData})")
$jsonData.get("name") ## <-- not working
#end
推荐答案
如果有人在 AWS API Gateway 中使用了速度,那么您可以使用 AWS'
If anyone using velocity in AWS API Gateway ends up here, you can use AWS'
$util.parseJson() 将字符串转换为 JSON.
$util.parseJson() to covert a string to JSON.
请务必注意您的字符串是使用单引号还是双引号.在解析字符串之前,您可能需要使用 $util.escapeJavaScipt.replaceAll().
Make sure you note if your string is using single or double quotes. You may need to use $util.escapeJavaScipt.replaceAll() before parsing the string.
这篇关于将字符串解析为 JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!