将Map转换为JSON在Java中

huangapple go评论75阅读模式
英文:

Converting Map to JSON in Java

问题

我有一个地图

Map<String, Application.RiskFactor> appRiskFactorsMap = app.getRiskFactors();

它里面有这种数据

{risk1=Application.RiskFactor(risk=risk1, question=question1,     
factor=true), risk2=Application.RiskFactor(risk=risk2,     
question=question2?, factor=true), 
risk3=Application.RiskFactor(risk=risk3, question=question3?, 
factor=true)}

我将其转换为JSON,并获得此输出。

{"risk1":{"risk":"risk1","question":"question1?","factor":"true"},"":
{"risk":"risk2","question":"question2?","factor":"true"},"risk3":
{"risk":"risk3","question":"question3?","factor":"true"}}

我有这个JSON转换器类
package system.referee.util;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

public final class JsonUtils {

private static final ObjectMapper MAPPER = new ObjectMapper();

static {
    // 反序列化时忽略未知字段
    MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

    // 序列化时忽略 null 和 Optional.EMPTY 字段
    MAPPER.setSerializationInclusion(JsonInclude.Include.NON_ABSENT);
}

public static <T> String toJson(T obj) {
    try {
        return MAPPER.writeValueAsString(obj);
    } catch (JsonProcessingException e) {
        return "";
    }
}

public static <T> T fromJson(String json, Class<T> type) {
    try {
        return MAPPER.readValue(json, type);
    } catch (JsonProcessingException e) {
        return null;
    }
}
}

我想以这种格式打印JSON

    {"risk":"risk1","question":"question1?","factor":"true"},
    {"risk":"risk2","question":"question2?","factor":"true"},
    {"risk":"risk3","question":"question3?","factor":"true"}

有没有办法实现这样的格式?我无法找到任何关于这个的帮助。非常感谢

英文:

I am having a map

Map&lt;String, Application.RiskFactor&gt; appRiskFactorsMap = app.getRiskFactors();

It has this data kind of in it

{risk1=Application.RiskFactor(risk=risk1, question=question1,     
factor=true), risk2=Application.RiskFactor(risk=risk2,     
question=question2?, factor=true), 
risk3=Application.RiskFactor(risk=risk3, question=question3?, 
factor=true)}

I am converting it into JSON and having this output.

{&quot;risk1&quot;:{&quot;risk&quot;:&quot;risk1&quot;,&quot;question&quot;:&quot;question1?&quot;,&quot;factor&quot;:&quot;true&quot;},&quot;&quot;:
{&quot;risk&quot;:&quot;risk2&quot;,&quot;question&quot;:&quot;question2?&quot;,&quot;factor&quot;:&quot;true&quot;},&quot;risk3&quot;:
{&quot;risk&quot;:&quot;risk3&quot;,&quot;question&quot;:&quot;question3?&quot;,&quot;factor&quot;:&quot;true&quot;}}

I have this JSON converter class
package system.referee.util;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

public final class JsonUtils {

private static final ObjectMapper MAPPER = new ObjectMapper();

static {
    // Ignore unknown fields while deserialization
    MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

    // Ignore null &amp; Optional.EMPTY fields while serialization
    MAPPER.setSerializationInclusion(JsonInclude.Include.NON_ABSENT);
}

public static &lt;T&gt; String toJson(T obj) {
    try {
        return MAPPER.writeValueAsString(obj);
    } catch (JsonProcessingException e) {
        return &quot;&quot;;
    }
}

public static &lt;T&gt; T fromJson(String json, Class&lt;T&gt; type) {
    try {
        return MAPPER.readValue(json, type);
    } catch (JsonProcessingException e) {
        return null;
    }
}
}

I want to print the JSON in this format

    {&quot;risk&quot;:&quot;risk1&quot;,&quot;question&quot;:&quot;question1?&quot;,&quot;factor&quot;:&quot;true&quot;},
    {&quot;risk&quot;:&quot;risk2&quot;,&quot;question&quot;:&quot;question2?&quot;,&quot;factor&quot;:&quot;true&quot;},
    {&quot;risk&quot;:&quot;risk3&quot;,&quot;question&quot;:&quot;question3?&quot;,&quot;factor&quot;:&quot;true&quot;}

is there any way to achieve that? I am unable to find any help with this. thanks a lot

答案1

得分: 1

你应该忽略键,只对值进行序列化:

JsonUtils.toJson(appRiskFactorsMap.values())

结果将是一个 JSON 数组

英文:

You should ignore keys and serialise only values:

JsonUtils.toJson(appRiskFactorsMap.values())

Result will be a JSON Array.

huangapple
  • 本文由 发表于 2020年10月1日 14:26:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/64150125.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定