java


How can i make a json file with java jsonGenerator?


I try to make this json format:
[{"x":1392440400000,"title":"!"},{"x":1392465600000,"title":"!"}]
I tried it out with the jsonGenerator
This is my code:
JsonFactory f = new JsonFactory();
StringWriter sw = new StringWriter();
JsonGenerator g = f.createJsonGenerator(sw);
while {
g.writeStartObject();
g.writeNumberField("x",111111);
g.writeStringField("title","!");
g.writeEndObject();
}
g.close();
return "["+sw.toString()+"]";
But my output is like that ist like that:
[{"x":1392440400000,"title":"!"} {"x":1392465600000,"title":"!"}]
Can anybody help me to make the correct Json output with a comma between the objects ?
You can use the ObjectMapper to generate the output.
So this could be something like this.
ObjectMapper mapper = new ObjectMapper();
HashMap<String, Object> data = new HashMap<String, Object>();
data.put("x", 1392440400000l);
data.put("title", "!");
HashMap<String, Object> data2 = new HashMap<String, Object>();
data2.put("x", 1392440400000l);
data2.put("title", "!");
List out = new ArrayList();
out.add(data);
out.add(data2);
String val = mapper.writeValueAsString(out);
I'm not using jackson, but for this specific scenario, you need your g.writeStartObject(); and g.writeEndObject(); inside the loop. (Because you're essentially trying to create an Array of Objects, right?)

Related Links

how to create jar file in netbeans
How to build a JPA converter for enumerations storing a key
Issue with encoding between java.text.Normalizer.Form.NFD and utf8 proc in C
Formatting types in Java
DefaultTableModel data moves from previously edited cell
How to return to code after I'm done with JavaFX scene
Inserting Newline character before every number occurring in a string?
Modifying an XML in Java
How to get output and give input from/to batch file using java
Java Swing - How to make the JButton's boundaries invisible?
Server Not Responding Using Put method with Spring web client
Why are cardlayout keys passed as Objects?
Java send image from client to server
Stopwatch resistant against system time changes
how to remove double quotes while reading CSV
How to trigger a JSP page refresh from server.?

Categories

HOME
maven
pandas
pdf
hook
comparison
lodash
maven-3
cakephp-2.5
webpack-2
constraint-programming
midi
collectd
gz
windows-server-2012
ups
try-catch
normalizr
crystal-reports-2008
blazemeter
android-widget
cas
dynamic-featured-image
wpfdatagrid
intel-pin
tooltipster
applozic
spring-mybatis
libssl
instant-messaging
stormpath
commit
form-data
ghost4j
http-referer
android-ble
event-driven
service-discovery
opshub
user-controls
mesos-chronos
pdf-reactor
segment
logfiles
azure-application-gateway
pango
veracode
magma
google-perftools
iso8601
account-kit
gnome-shell-extensions
cubes
angular-strap
color-picker
heidisql
android-textview
google-web-starter-kit
superstack
essence
revolution-r
itextpdf
kendonumerictextbox
xna-4.0
rgeo
sframe
angular-local-storage
unity5.2.3
c++03
dd
coldbox
angularjs-ng-click
asp.net-dynamic-data
relocation
socketexception
castle
google-reader
datagridviewcolumn
apc
google-cloud-save
dotnetnuke-5
cos
gwt-rpc
yui-compressor
anonymous-methods
hosts-file
wse3.0
eclipse-templates
zpt
recent-documents
appendto
oncheckedchanged
remember-me
yslow
coda-slider
infrastructure
kdbg
premature-optimization
wsdl.exe

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile