我在输入隐藏字段中保存了以下JSON字符串,
[{"pro":{"draft":{"create":"1"},"submitted":{"edit":"1","delete":"0"}},"ind":{"draft":{"create":""},"submitted":{"edit":"","delete":""}}}]
当我尝试从隐藏文本字段中检索JSON时,
JSON.stringify($('#SecurityJSON').val()) //returns "[{"
但是如果尝试直接将字段内容传递给JSON stringify,那么它将返回正确的JSON,
JSON.stringify([{"pro":{"draft":{"create":"1"},"submitted":{"edit":"1","delete":"0"}},"ind":{"draft":{"create":""},"submitted":{"edit":"","delete":""}}}]) //works well
我是否需要解析/格式化文本字段以检索正确的JSON?
答案 0 :(得分:1)
这个解决方案怎么样?希望它有所帮助!
var data = {}
data = $.parseJSON($('#SecurityJSON').val());
console.log(data[0]);

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<input type="hidden" value='[{"pro":{"draft":{"create":"1"},"submitted":{"edit":"1","delete":"0"}},"ind":{"draft":{"create":""},"submitted":{"edit":"","delete":""}}}]'
id="SecurityJSON">
&#13;