I got the solution by chance. If you make the Javascript VM create a new string, by appending a blank string, then this can now be parsed as JSON. I don't know why.
On Feb 25, 12:33 pm, Miguel Paraz <[email protected]> wrote: > Hi, > I'm trying to pass a JSON structure from my Java code to JavaScript, > in a WebView. The HTML file uses the json2.js parser: > > <html> > <head> > <title>JSON Test</title> > <script type="text/javascript" src="./json2.js"/> > <script type="text/javascript"> > function loader() { > var jsonData = window.webConnector.load(); > document.getElementById('original').innerHTML = jsonData; > > var a = JSON.parse(jsonData); > document.getElementById('jsonOutput').innerHTML = a;} > > </script> > </head> > <body onload="loader()"> > <div id="test">Test</div> > > Original: > <div id="original">Original could not be loaded</div> > > JSON Output: > <div id="jsonOutput">JSON Could not be Parsed</div> > </body> > </html> > > The Java: > > public class JsonActivity extends Activity { > @Override > public void onCreate(Bundle savedInstanceState) { > super.onCreate(savedInstanceState); > setContentView(R.layout.main); > > final WebView webView = (WebView) findViewById(R.id.webView); > > webView.getSettings().setJavaScriptEnabled(true); > webView.addJavascriptInterface(this, "webConnector"); > webView.loadUrl("file:///android_asset/json.html"); > } > > public String load() { > return "{\"frm\":[[\"name\",\"Miguel\"],[\"age\",\"34\"],[\"rating > \",\"low\"],[\"comments\",\"hungry\"]]}"; > } > > } > > The WebView output is: the JSON text is loaded, but the parsed version > is not. > > In LogCat: > 02-25 12:20:17.508: DEBUG/WebCore(811): Console: JSON.parse line: 475 > source: file:///android_asset/json2.js > > which is because of a thrown exception. > > If the JSON text is inlined into the HTML, then it works fine. > > Creating the JSON in the HTML, passing it to Java and parsing it using > JSONObject, is fine. > > Is there a security restriction here? Thanks. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~----------~----~----~----~------~----~------~--~---

