隐藏文本字段中的JSON解析问题

时间:2016-11-17 03:09:02

标签: jquery json

我在输入隐藏字段中保存了以下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?

1 个答案:

答案 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;
&#13;
&#13;

相关问题