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?

1
  • If he's asking what it sounds like he's asking, this isn't a dupe.
    – Jherico
    Nov 21, 2012 at 21:43

1 Answer 1

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.

2
  • Yes. Thank you. You have understood correctly. Is there such a JavaScript parsing engine available for java? Nov 21, 2012 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. Nov 21, 2012 at 22:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.