I’m passing an json object to my js file:
<script type="text/javascript" src="../js/main.js">
{ lang: 'de' }
</script>
Trying to get the json object in main.js:
var scriptContent = $('script').filter(function () { return this.src.match(/js\/main\.js/g); }).html(),
params = $.parseJSON( scriptContent.replace(/"/g, '"') );
But this results in the following error: JSON.parse: expected property name or '}'
As you can see I already tried replacing " by "
I tried { lang: "de" } instead of { lang: 'de' }
How to get this working?
Your snippet is incorrect JSON. Property keys have to be quoted too.
To quickly produce valid JSON (for testing purposes?), use
JSON.stringify( some_object ), eg: