Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I create classes from json data similar to jaxb

Tags:

java

json

xml

jaxb

So my code is a client of an api, the data is returned as xml and Ive been able to create valid xsd file from some examples of that xml and then generate some JAXB classes from the schema so my code can now load and work with the xml data without ever having to work directly with Xml.

But unfortunately in the latest version of the api they have dropped xml support and only return json. Is there a json process I can do analogous to my xml process ?

If its not possible from a schema are there solutions so that if I manually create my json classes, I can them use them to automatically martial in raw json data, so at least I only have to deal with json once.

EDIT:Maybe https://github.com/ko5tik/jsonserializer would be useful

UPDATE:FYI so I looked at jsonschema2pojo but that only creates pojos from a schema, and I didnt actually have a json schema, just the actual json. I had a go at creating a schema from the example json I had but didnt get it working for al but the simplest example.

I then looked at http://wiki.fasterxml.com/JacksonInFiveMinutes , Jackson would have been able to use the pojos created by jsonschema2pojo if Id managed to get it working. So I then tried following the example and created a POJO based on the json data I had and it was quite easy, then I then tried full data binding using this and it worked first time.

So in summary Im going to use Jackson for dealing with json returned by the webservice, I'll have to manually create a POJO for each entity but at least it works.

like image 835
Paul Taylor Avatar asked Nov 04 '11 11:11

Paul Taylor


People also ask

Does JAXB support JSON?

Java example to convert Java objects to JSON string or write JSON to file. This example uses MOXy along with JAXB to marshal Java object to JSON. MOXy implements JAXB allowing developers to provide their mapping information through annotations as well as provide many rich features which JAXB doesn't provide by default.

How do you Unmarshal JSON with JAXB?

Unmarshal JSON to Java Object: Create a JaxBContext using the Employee class then read the provided JSON string and convert it back to the “employee” Java object using Unmarshaller object with following two properties: MEDIA_TYPE – Determine the provided input media type (JSON, XML).

How do you make JAXB classes from XSD?

For Eclipse STS (3.5 at least) you don't need to install anything. Right click on schema. xsd -> Generate -> JAXB Classes. You'll have to specify the package & location in the next step and that's all, your classes should be generated.


2 Answers

As to answer, maybe try "jsonschema2pojo".

But similar questions have already been asked before:

  • How can I parse a JSON schema and create a default json object or generate a java class?
  • Generate Java class from JSON?
  • Is there a tool to generate a JSON schema from an XML schema through Java?
like image 98
StaxMan Avatar answered Sep 30 '22 02:09

StaxMan


If you want to create POJOs from a sample JSON file, I've created a (hacky) ruby script that can help. I've added more details in another answer here: https://stackoverflow.com/a/8585423/1109175

You can find the code on github: https://github.com/wotifgroup/json2pojo

like image 20
Chris R Avatar answered Sep 30 '22 01:09

Chris R