Dänu
Dänu

Reputation: 5929

jQuery parseJSON and functions inside the json string

I'm trying the following:

var objJson = jQuery.parseJSON('"myFunc": function(){ ... }');

This fails. Now my question(s): Why does it fail and how can I achieve what I'm trying?

Thank you in advance!

Upvotes: 0

Views: 606

Answers (2)

Álvaro González
Álvaro González

Reputation: 146460

You seem to have a misconception about what JSON is. JSON is a simple data format with a syntax that resembles JavaScript objects. It's so simple that spec fits a single page. If you have functions it it, it isn't JSON. Thus there is no point in using JSON tools.

Now, the usual way to inject external JavaScript code into an HTML document is the <script> tag:

<script type="text/javascript" src="/path/to/code.js">

Upvotes: 1

D&#228;nu
D&#228;nu

Reputation: 5929

ok, the following worked for me (should have googled a bit longer):

var o = eval('(' + '"myFunc": function(){ ... }' + ')')

Upvotes: 0

Related Questions