I am working on tablet web app project, but I don't know how to create JSON file getting out info with Java connecting to database. Info is getting through of Store Procedure and data return with List<Object obj> or with ResultSet. Thanks.
3 Answers
Use Jackson library to convert any Java model object into JSON.
ObjectMapper mapper = new ObjectMapper();
mapper.writeValue(new File("c:\\modelObj.json"), modelObj);
or
String s = mapper.writeValueAsString(modelObj);
Comments
A json file is text - so retrieve just as you would any string. If you were using a result set you could use ResultSet.getString(). Then you probably will want to deserialize it into either a domain object or some form of json object using a json parsing library.
There are lots of different libraries out there for doing this, for instance Jackson or Gson. I personally prefer gson - probably just because I'm more familiar with it and I find the API quite straight forward.