admin管理员组文章数量:1424942
I store JSON data in HTML hidden fields on the server side. Then I'd like to retrieve that data using Javascript and JQuery on the client side. The problem is that I get a JSON string instead of a JSON object.
This is my code on the server side:
<form id="data" style="display: none;">
<input id="channels" type="hidden" tal:attributes="value python: view.context['ChannelManager'].toJSON(view.channels.values())" />
<input id="mediaGroups" type="hidden" tal:attributes="value python: view.context['MediaGroupManager'].toJSON(view.mediaGroups.values())" />
</form>
This is my code on the client side:
copy.channelList = new ChannelTest();
copy.channelList.fromJSONObjectAll($("#data input[id=channels]").val())
So I get JSON string instead of JSON object from this, $("#data input[id=channels]").val().
How could I get JSON object without converting JSON string in JSON object?
Thanks in advance!
I store JSON data in HTML hidden fields on the server side. Then I'd like to retrieve that data using Javascript and JQuery on the client side. The problem is that I get a JSON string instead of a JSON object.
This is my code on the server side:
<form id="data" style="display: none;">
<input id="channels" type="hidden" tal:attributes="value python: view.context['ChannelManager'].toJSON(view.channels.values())" />
<input id="mediaGroups" type="hidden" tal:attributes="value python: view.context['MediaGroupManager'].toJSON(view.mediaGroups.values())" />
</form>
This is my code on the client side:
copy.channelList = new ChannelTest();
copy.channelList.fromJSONObjectAll($("#data input[id=channels]").val())
So I get JSON string instead of JSON object from this, $("#data input[id=channels]").val().
How could I get JSON object without converting JSON string in JSON object?
Thanks in advance!
Share Improve this question asked Nov 29, 2010 at 17:24 bribonbribon 2331 gold badge7 silver badges17 bronze badges 4- 1 What do you mean by "get JSON object without converting the string"? – casablanca Commented Nov 29, 2010 at 17:26
- I posted what I think you want but yeah that part is confusing. You are asking how to convert a json string to an object, without converting a json string to an object... – Alex Wayne Commented Nov 29, 2010 at 17:29
- When I fill the HTML inputs the data is JSON object, not a JSON String. However, when I get that data on the client side, it's JSON string instead of JSON Object. I'd like to avoid to convert that JSON string in JSON object. – bribon Commented Nov 29, 2010 at 17:33
- try editing this: input[id$=channels] and let me know! – Andrea Turri Commented Nov 29, 2010 at 17:35
1 Answer
Reset to default 6JSON.parse(jsonString);
For older browsers that don't have native JSON support, you can simply include json2.js and this will bee a usable function.
Or you can skip this step with some server side scripting. You can simply write the JSON into a script tag. It is parsed implicitly by the script tag instead, as raw javascript.
<script type="text/javascript">
var myObj = <%= myJsonString %>;
console.log('we got this value: '+ myObj.myValue);
</script>
本文标签: javascriptRetrieve JSON data from a hidden inputStack Overflow
版权声明:本文标题:javascript - Retrieve JSON data from a hidden input - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745390290a2656566.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论