2

I'm coming to Java from a PHP background, and am surprised to see that JSON to object conversion is so constrained. In all the Jackson tutorials I came across, it looks like the object to be read needs to be pre-defined. Thus, if my data is in, say, JSON API format, I need to write boilerplate code to strip out everything except the "data" part, and then somehow convert all the strings into objects one by one.

I really miss PHP's json_decode function, which will read any JSON and give you a PHP object to play with. It also builds the necessary structure into the object, adding arrays and sub-objects as needed. Of course I understand that Java is a compiled language, but I'm wondering how this can be made easier.

2
  • 1
    If I understand you correctly you can check google-gson. Commented Nov 8, 2016 at 6:23
  • 1
    @S.I. Yes, this is exactly what I was searching for! Creating object on the fly, without having to define classes, is very convenient! Commented Nov 8, 2016 at 6:26

1 Answer 1

1

As a strongly typed language Java often has less of these "just give it to me"-type of functionalities, but that doesn't mean they don't exist. Even Jackson can deserialize JSON without a predefined schema, giving you Maps and Lists instead of domain objects.

Just remember that if you're working on "real" projects, there are plenty of advantages from having the schemas defined. They weren't invented to annoy you, but to make sure that you can trust your data being in the correct form (or find out early if it's not).

Sign up to request clarification or add additional context in comments.

3 Comments

Yes, some great words of wisdom there. I'm more interested in a quick-and-dirty program as of now, but I see how even at this level having a schema is a good thing.
Indeed, sometimes you just want to get things done and then polish it up properly when you've got the details worked out.
Yeah, but I guess typing 10-20 extra lines for class definitions shouldn't hurt. :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.