2016-11-17 100 views
0

解析問題,JSON從我有以下保存在輸入隱藏字段JSON字符串隱藏的文本字段

[{"pro":{"draft":{"create":"1"},"submitted":{"edit":"1","delete":"0"}},"ind":{"draft":{"create":""},"submitted":{"edit":"","delete":""}}}] 

當我正在嘗試從隱藏的文本字段像JSON,

JSON.stringify($('#SecurityJSON').val()) //returns "[{" 

但是,如果嘗試直接傳遞字段內容爲JSON字符串化然後返回適當的JSON,

JSON.stringify([{"pro":{"draft":{"create":"1"},"submitted":{"edit":"1","delete":"0"}},"ind":{"draft":{"create":""},"submitted":{"edit":"","delete":""}}}]) //works well 

我需要解析/格式化文本字段以檢索正確的JSON?

+1

你有沒有正確轉義輸入的'「'價值呢? –

+0

感謝您指出,這是問題了。 – Rishi

回答

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">

+0

完美的一個!謝謝! – Rishi

+0

@Rishi你」歡迎:) – HenryDev