10

Here's my desired output

{"node":{"type":"community","field_incentives":{"und":[{"value":"fun"},{"value":"nice"}]},"field_community_email":{"und":[{"value":"[email protected]"}]}}}

Here's my code but it does not seem to generate the output above. If there's a better and simpler way to do this, please let me know. Thanks

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
JsonFactory f = new JsonFactory();
JsonGenerator g = f.createJsonGenerator(outputStream);
g.writeStartObject();
g.writeObjectFieldStart("node");
g.writeStringField("type", "community");
g.writeObjectFieldStart("field_incentives");
g.writeFieldName("und");
g.writeStartArray();
???I don't know how to make [{"value":"fun"},{"value":"nice"}]
g.writeEndArray();
g.writeEndObject();
g.close();
3
  • Why don't you just write the JSON string? Commented Sep 26, 2012 at 3:26
  • You are doing it wrong. Either just write the String by yourself, or use a POJO and serialize it using Jackson. Commented Sep 26, 2012 at 3:31
  • 8
    I disagree with Nishant. That only makes sense if you're serializing already-existing domain objects. Why would you create a new type hierarchy just to pump out some JSON?? Commented Sep 26, 2012 at 3:55

2 Answers 2

15

I simply write line by line to your output json file Reference JsonGenerator. Hope it will help.

import java.io.File;
import java.io.IOException;
import org.codehaus.jackson.JsonEncoding;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.map.JsonMappingException;

public class CopyOfJacksonStreamExample {
   public static void main(String[] args) {

     try {

    JsonFactory jfactory = new JsonFactory();

    /*** write to file ***/
    JsonGenerator jGenerator = jfactory.createJsonGenerator(new File(
            "c:\\user.json"), JsonEncoding.UTF8);
    jGenerator.writeStartObject(); // {

    jGenerator.writeObjectFieldStart("node"); // node: {
    jGenerator.writeStringField("type", "community"); // "type" : "community"

    jGenerator.writeObjectFieldStart("field_incentives"); // "field_incentives" : {
    jGenerator.writeFieldName("und"); // "und" :
    jGenerator.writeStartArray(); // [
    jGenerator.writeStartObject(); // {
    jGenerator.writeStringField("value", "fun"); // "value" : "fun"
    jGenerator.writeStringField("value", "nice"); // "value" : "nice"
    jGenerator.writeEndObject(); // }
    jGenerator.writeEndArray(); // ]
    jGenerator.writeEndObject(); // } end of field_incentives

    jGenerator.writeObjectFieldStart("field_community_email"); // "field_community_email" : {
    jGenerator.writeFieldName("und"); // "und" :
    jGenerator.writeStartArray(); // [
    jGenerator.writeStartObject(); // {
    jGenerator.writeStringField("value", "[email protected]"); // "value" : "fun"
    jGenerator.writeEndObject(); // }
    jGenerator.writeEndArray(); // ]
    jGenerator.writeEndObject(); // } end of field_community_email

    jGenerator.writeEndObject(); // } end of node
    jGenerator.writeEndObject(); // }

    jGenerator.close();

     } catch (JsonGenerationException e) {

    e.printStackTrace();

     } catch (JsonMappingException e) {

    e.printStackTrace();

     } catch (IOException e) {

    e.printStackTrace();

     }

   }

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

1 Comment

@Andy did this answer solved your problem? If so, accept it please
1

Using POJOs
Main

public class Main {

    private Node node;

    // getters/setters  

Node

public class Node {

    private String type;

    private FieldIncentives field_incentives;

    private FieldIncentives field_community_email;

    // getters/setters  

FieldIncentives

public class FieldIncentives {

    private List<Holder> und;

    // getters/setters  

Holder

public class Holder {

    private String value;

    // getters/setters  

Usage

final ObjectMapper o = new ObjectMapper();
final Main m = o.readValue(new File("exampleJson.json"), Main.class);

Comments

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.