Why is it horribly time-wasting to work with JSON on non-dynamic programming languages?
-
For example in Java(specifically, Android development) programmers have to write a new class whenever they try to parse a JSON variable. And on other non-OOP non-dynamic programming languages it gets even more complicated and time-wasting by either re-parsing or creating a new file for each JSON document. While on dynamic programming languages - such as PHP - it becomes as easy as calling a single function, just like this: $parsed = json_decode($document); Why does it take too much time to write code that parses such a simple(?) format? Why are hash tables not being used to parse JSON documents ? wouldn't that make it bloody easy to parse JSON documents in C, Java, and any other programming language by simply representing the whole document as a nested hash table?
-
Answer:
The premise is incorrect. In a modern language like Scala, exactly the same techniques are available to work with JSON, but with Scala's advantages of better efficiency and static type checking. The same is true of other modern compiled languages.
Toby Thain at Quora Visit the source
Other answers
There is JSON pointer specification (RFC 6901) which allows accessing JSON content using XPath-like syntax. There are implementations of it in various languages. Pick a library and that might ease a lot of things. --- original answer --- There is no reason to. GSON requires reflection to parse JSON into class instances, which, IMHO, is truly a waste of time in the most cases. JSONs are dictionaries by nature, and to use them as dicts is the right way to go. Python json.loads is the best friend. However more on the Java side.. Even if we have HashMap<String, ...> to work with, it's still a pain. The best thing in XML processing that is still missing from JSON is XPath. It uses a separate, very compact / expressive / host language neutral DSL to navigate through the tree structure represented by XML and I don't see why it would not be possible to apply it (or invent anything similar / better) for JSON.
Ryan Gao
I do agree with you that parsing JSON in a language with Java is more work than necessary, but it doesn't have to do with whether the language is dynamic or not. It has to do with the OOP paradigm in Java that you want a class to model everything, even when you could do it in a more simple way. I imagine that's why you need to create a bucketload of new classes. There's no fundamental reason that would prevent a hashtable implementation of a JSON parser. I would be surprised if there isn't one and if there really isn't one, that sounds like a great open-source project you could do. Another interesting alternative would be to have a code generator of classes, given a "reference" JSON file. I got this idea from F# Type Providers where you can give it a sample JSON/XML/whatever file and it will infer the types for you http://fsharp.github.io/FSharp.Data/library/JsonValue.html. For example, you can write : let info = JsonValue.Parse("""{ "name": "Tomas", "born": 1985, "siblings": [ "Jan", "Alexander" ] }""") And now you have an object with name, born and siblings property.
Rudi Chen
Related Q & A:
- What programming languages are 'general purpose' and 'domain specific?Best solution by Programmers
- What are the best programming languages to learn now?Best solution by Software Quality Assurance & Testing
- What programming languages should I learn for Web development?Best solution by Yahoo! Answers
- Why won't my Time Warner Cable remote work?Best solution by Yahoo! Answers
- From what time could i work at a grocery store?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.