1

Possible Duplicate:
How to parse JSON in Android

I am writing an Android app where I'm getting a .js (JavaScript) file from a url and I want to read its contents. Is there any way to convert this file to a JSONArray or JSONObject? Or a direct way to parse the .js file itself?

Community
  • 1
  • 1
Rameez Hussain
  • 6,414
  • 10
  • 56
  • 85

1 Answers1

2

If you mean can you create a data structure out of raw JavaScript, then no.

You can't convert a string of arbitrary JavaScript to JSON except in the degenerate sense that the entire string is a valid JSON item of type 'string'. This is because the syntax of JS covers a much larger domain than JSON. For example, what kind of JSON structure would you expect to represent the following JS?

while (true) { }

If your intent is to traverse the JS and pull data structures out of it, you're probably going to need something like a full JavaScript parsing engine.

If on the other hand you've phrased the question badly and the '.js' file you're fetching is really a JSON file, then the question is answered in the marked duplicate.

Jherico
  • 28,584
  • 8
  • 61
  • 87
  • Yes. Thank you. You have understood correctly. Is there such a JavaScript parsing engine available for java? – Rameez Hussain Nov 21 '12 at 21:52
  • Nope. It's not. I already know how to parse a JSON file. I know there is a difference. Despite the bad phrasing, and what it may imply. ;) Thanks to your short explanation I know what to look for. – Rameez Hussain Nov 21 '12 at 22:03