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
Add a ment  | 

1 Answer 1

Reset to default 6
JSON.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