I found that many of the tutorials on the net and here on SO also refer to net.sf.json library to convert an xml file to json object.
But, I want an alternative preferably using Gson. Is it possible? I don't have well defined Java Classes for the XML file. But, I just want to convert the xml file to com.google.gson.JsonObject. How to achieve it?
I've done the same using JAXB to convert my xml to an object, and passing the object to gson.
I know it takes one additional step, but that worked convenient for me.
Upon converting xml to jaxb see also:
Use JAXB to create Object from XML String
Related
I would like to extract only the data present in a Json to JsonForm using Java.
Which framework can I use to this operation?
I've found the answer. Don't let the ugly aspect of JsonForm deceive you. I don't know if this apply for all cases, but I just had to extract the Json object that contains only the data. This can be done using any common Json parser, as like as Gson.
I am looking to automate validating JSON instances against XML instances. What would be a good way to achieve this? I was thinking of mapping fields but then again this would just solve the problem say for one instance and the script would need to be updated for each instance.
Are there any libraries in Java that can aid in this?
No, use XML Schema (XSD) for validating XML; use JSON Schema for validating JSON.
If you're looking to parse from XML to JSON (unmarshal) or serialize JSON to XML (marshal), see JSONIX.
You may be using validate in an unconventional sense which would be satisfied by parsing from XML to JSON using JSONIX for comparison with other JSON.
What is the easiest to use JSON Java library to parse a JSON (It's structure may vary so I can't mapp it to a Java class as I seen multiple libraries do it) update some elements in an array in this JSON and then save it back to disk?
There are so many libraries for JSON in Java (Gson, Jackson, etc.) and so complex that seem like a total overkill for what i need as opposed to other programming languages.
What's the most straightforward one to use? (that maybe also has a few examples on how to do this)
I have had great success with Json-Simple.
You can check it out here.
I used it to parse 1.5 Million Twitter Streaming data (which is in JSON).
You may find some sample code here on my blog.
You can use java-json jar. The docs for this jar can be found here
I use net.sf.json to do this.
here is an example :
String fromFile="{\"he\",\"hello\"}" //read from file instead
JSONObject object=(JSONObject)JSONSerializer.toJSON( fromFile );
object.put("he", "hello2");
System.out.println(object);
output:
{"he":"hello2"}
I got an XML string with many elements and I wonder is there a way to parse it into some kind of Java bean (Properties?) if I do not have a java bean defined? Mapping all the elements to java bean properties manually can be time-consuming, so I am looking for some kind of 'optimization' here .
Any thoughts?
If it's one level deep you can use jackson XmlMapper and map it to a Map'<'String,String>
post an example of the XML if you want more help.
I want to read data from the database, convert it to docs (JSON) using Java.
Thanks
GSON is a Java library from Google to convert Java objects to JSON. You can simply pass a Java object to the library function and it will return a JSON string.
Download: http://code.google.com/p/google-gson/
Example: http://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/
I have used the library from http://www.json.org, but the whole thing seems to be tedious to me. GSON is simple to use IMHO.
I would use JSONObject class:
http://www.json.org/javadoc/org/json/JSONObject.html
or you can build the string.