简体   繁体   中英

Java - Convert to correct json format using Jackson

I'm using Jackson to write a Java object to Json.

This is wath I am getting

{
"obj": [{
    "Id": 1,
    "type": "type1",
    "properties": [{
        "name": "PropN",
        "value": "ValN"
    }]
}, {
    "id": 2,
    "type": "type",
    "properties": [{
        "name": "Prop3",
        "value": "Val3"
    }]
}]

}

This is what i need to get:

{
    "obj": [{
            "id": 1,
            "type": "type",
            "properties": {
                "name": "Eb1"
            }
        },
        {
            "id": 2,
            "type": "type",
            "properties": {
                "name": "Eb2"
            }
        }
    ]
}

I dont know how to get the properties name and value "tags" from the json and also, remove the properties array to a list.

My Properties array is a simple POJO, and the Obj class has an ArrayList of properties.

Can someone tell me how to get this done?

Thanks.

You can use quicktype to generate the exact types you need. Here's what it produces with the JSON you require:

// Converter.java

// https://stackoverflow.com/questions/49055781/java-convert-to-correct-json-format-using-jackson
//     import io.quicktype.Converter;
//
// Then you can deserialize a JSON string with
//
//     StackOverflow data = Converter.fromJsonString(jsonString);

package io.quicktype;

import java.util.Map;
import java.io.IOException;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.core.JsonProcessingException;

public class Converter {
    // Serialize/deserialize helpers

    public static StackOverflow fromJsonString(String json) throws IOException {
        return getObjectReader().readValue(json);
    }

    public static String toJsonString(StackOverflow obj) throws JsonProcessingException {
        return getObjectWriter().writeValueAsString(obj);
    }

    private static ObjectReader reader;
    private static ObjectWriter writer;

    private static void instantiateMapper() {
        ObjectMapper mapper = new ObjectMapper();
        reader = mapper.reader(StackOverflow.class);
        writer = mapper.writerFor(StackOverflow.class);
    }

    private static ObjectReader getObjectReader() {
        if (reader == null) instantiateMapper();
        return reader;
    }

    private static ObjectWriter getObjectWriter() {
        if (writer == null) instantiateMapper();
        return writer;
    }
}

// StackOverflow.java

package io.quicktype;

import java.util.Map;
import com.fasterxml.jackson.annotation.*;

public class StackOverflow {
    private Obj[] obj;

    @JsonProperty("obj")
    public Obj[] getObj() { return obj; }
    @JsonProperty("obj")
    public void setObj(Obj[] value) { this.obj = value; }
}

// Obj.java

package io.quicktype;

import java.util.Map;
import com.fasterxml.jackson.annotation.*;

public class Obj {
    private long id;
    private String type;
    private Properties properties;

    @JsonProperty("id")
    public long getID() { return id; }
    @JsonProperty("id")
    public void setID(long value) { this.id = value; }

    @JsonProperty("type")
    public String getType() { return type; }
    @JsonProperty("type")
    public void setType(String value) { this.type = value; }

    @JsonProperty("properties")
    public Properties getProperties() { return properties; }
    @JsonProperty("properties")
    public void setProperties(Properties value) { this.properties = value; }
}

// Properties.java

package io.quicktype;

import java.util.Map;
import com.fasterxml.jackson.annotation.*;

public class Properties {
    private String name;

    @JsonProperty("name")
    public String getName() { return name; }
    @JsonProperty("name")
    public void setName(String value) { this.name = value; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM