如何使用json对象名称作为增量值?

时间:2019-06-23 11:29:45

标签: java json jackson

我是JSON的新手,我需要从Jackson API输出JSON的特定格式。这是实际需要的输出:

    {
        "0": {
            "symbol": "B",
            "count": 2,
            "symbolIndex": [0, 0]
        },
        "1": {
            "symbol": "B",
            "count": 2,
            "symbolIndex": [0, 0]
        },
        "2": {
            "symbol": "B",
            "count": 2,
            "symbolIndex": [0, 0]
        }
    }

请考虑对象名称可以更改(0,1,2,3,4,5....)并取决于要求,并且这些名称只能递增。我如何使用对象使用Jackson API在Java中生成此JSON输出?

更新

所以我从汤姆那里得到了答案,完整的代码如下:

MainClass.java

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class MainClass {
    public static void main(String[] args) {
        SymbolCounts symbolCounts = new SymbolCounts();
        symbolCounts.add("0", new MySymbol("A", 2, new int[]{1,1}));
        symbolCounts.add("1", new MySymbol("B", 2, new int[]{1,1}));
        symbolCounts.add("2", new MySymbol("C", 2, new int[]{1,1}));
        String str = getJSONResponse(symbolCounts);
        System.out.println(str);
    }
    protected static String getJSONResponse(SymbolCounts responseData) {
        String jsonStringResponse = "";
        try {
            ObjectMapper mapper = new ObjectMapper();
            jsonStringResponse = mapper.writeValueAsString(responseData);
        } catch (JsonProcessingException jsonProcessingException) {
            System.out.println(jsonStringResponse);
        }
        return jsonStringResponse;
    }
}

SymbolCounts.java

import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.HashMap;
import java.util.Map;
import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL;
@JsonInclude(NON_NULL)
@JsonIgnoreProperties(ignoreUnknown=true)
public class SymbolCounts {
    @JsonProperty("symbolCounts")
    private Map<String, MySymbol> symbolMap = new HashMap<String, MySymbol>();
    @JsonAnySetter
    public void add(String key, MySymbol value) {
        symbolMap.put(key, value);
    }
    public Map<String, MySymbol> getSymbolMap() {
        return symbolMap;
    }
    public void setSymbolMap(Map<String, MySymbol> symbolMap) {
        this.symbolMap = symbolMap;
    }
    @Override
    public String toString() {
        return "SymbolCounts{" +
                "symbolMap=" + symbolMap +
                '}';
    }
}

MySymbol.java

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import java.util.Arrays;
import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL;
@JsonInclude(NON_NULL)
@JsonIgnoreProperties(ignoreUnknown=true)
public class MySymbol {
    private String symbol;
    private int count;
    private int[] symbolIndex;
    public MySymbol() {
    }
    public MySymbol(String symbol, int count, int[] symbolIndex) {
        this.symbol = symbol;
        this.count = count;
        this.symbolIndex = symbolIndex;
    }
    public String getSymbol() {
        return symbol;
    }
    public void setSymbol(String symbol) {
        this.symbol = symbol;
    }
    public int getCount() {
        return count;
    }
    public void setCount(int count) {
        this.count = count;
    }
    public int[] getSymbolIndex() {
        return symbolIndex;
    }
    public void setSymbolIndex(int[] symbolIndex) {
        this.symbolIndex = symbolIndex;
    }
    @Override
    public String toString() {
        return "LineID{" +
                "symbol='" + symbol + '\'' +
                ", count=" + count +
                ", symbolIndex=" + Arrays.toString(symbolIndex) +
                '}';
    }
}

1 个答案:

答案 0 :(得分:1)

您可以使用地图和@JsonAnySetter。

您的高级班级看起来像:

torch.relu

您的MySymbol类将是:

private Map<String, MySymbol> symbolMap;

@JsonAnySetter
public void add(String key, MySymbol value) {
    symbolMap.put(key, value);
}

然后,您的最终结果将是一个映射,其中的键是作为字符串的数字值,而值是您的符号对象。