spring messaging将json字符串转换为map

时间:2014-09-09 15:37:14

标签: java json spring spring-integration

使用Spring Integration,我有一个json字符串(见下文)和以下代码:

public SomethingBean convert(Message<?> inMessage) {...}

Json字符串

{
    "addressIdentification": {
        "identifierType": "nemtom",
        "addressIdentifier": "eztse"
    },
    "postcode": "BH1EH",
    "country": "5"
}

我想使用以下方法签名:

public SomethingBean convert(Message<Map<String, ?>> inMessage) {...}

是否可以自动将json字符串转换为Map?

谢谢, 诉

3 个答案:

答案 0 :(得分:3)

只需使用Spring Integration开箱即用的组件:

<json-to-object-trnsfrormer type="java.util.Map"/>
SomethingBean调用之前

答案 1 :(得分:1)

使用任何JSON解析库,例如GSONJackson,并将其转换为Java Object。

GSON:

String jsonString = "{\"addressIdentification\":{\"identifierType\":\"nemtom\",\"addressIdentifier\":\"eztse\"},\"postcode\":\"BH1EH\",\"country\":\"5\"}";

Type type = new TypeToken<Map<String, Object>>() {}.getType();
Map<String, Object> data = new Gson().fromJson(jsonString, type);
System.out.println(new GsonBuilder().setPrettyPrinting().create().toJson(data));

for(Map.Entry<String, Object> entry:data.entrySet()){
    System.out.println(entry.getKey()+":"+entry.getValue());
}

杰克逊:

String jsonString = "{\"addressIdentification\":{\"identifierType\":\"nemtom\",\"addressIdentifier\":\"eztse\"},\"postcode\":\"BH1EH\",\"country\":\"5\"}";

JSONObject jsonObject = new JSONObject(jsonString);
System.out.println(jsonObject);

JSONObject addressIdentification = jsonObject.getJSONObject("addressIdentification");
System.out.println("identifierType:" + addressIdentification.get("identifierType"));
System.out.println("addressIdentifier:" + addressIdentification.get("addressIdentifier"));

System.out.println("postcode:" + jsonObject.get("postcode"));
System.out.println("country:"+jsonObject.get("country"));

输出:

identifierType:nemtom
addressIdentifier:eztse
postcode:BH1EH
country:5

Read more...

答案 2 :(得分:0)

您可以使用以下代码

package com.mboot.generator.models;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * Container for all request keys and values values maybe single value or list
 */
public class OptionParameter {
private Map<OptionKey, Object> parameter = new HashMap<>();

public OptionParameter add(OptionKey OptionKey, Object value) {
    if (value != null && parameter.get(OptionKey) == null)
        parameter.put(OptionKey, value);
    return this;
}

public Object get(OptionKey OptionKey) {
    return parameter.get(OptionKey);
}

public void iterator(ParameterIterator iterator) {
    parameter.entrySet().stream().sorted((k, v) -> k.getKey().name().compareTo(v.getKey().name()))
            .forEach((e) -> iterator.parameter(e.getKey(), e.getValue()));
}

public interface ParameterIterator {
    void parameter(OptionKey key, Object value);
}

@Override
public String toString() {
    return "OptionParameter{" + "parameter=" + parameter + '}';
}

@Override
public boolean equals(Object o) {
    if (this == o)
        return true;
    if (o == null || getClass() != o.getClass())
        return false;
    OptionParameter parameter1 = (OptionParameter) o;
    return Objects.equals(parameter, parameter1.parameter);
}

@Override
public int hashCode() {
    return Objects.hash(parameter);
}

}

您的消息转换器应如下所示

package com.mboot.generator.utils;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.converter.Converter;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.mboot.generator.models.OptionKey;

import lombok.SneakyThrows;

public class StringToMapConverter implements Converter<String, Map<OptionKey, Object>> {

@Autowired
private ObjectMapper objectMapper;
TypeFactory factory = TypeFactory.defaultInstance();

@Override
@SneakyThrows
public Map<OptionKey, Object> convert(String source) {
//  Map<OptionKey, Object> result = objectMapper.readValue(source,
//  factory.constructMapType(Map.class, OptionKey.class, Object.class));

    JavaType javaType =        objectMapper.getTypeFactory().constructParametrizedType(Map.class, OptionKey.class, Object.class);


    return  objectMapper.readValue(source,javaType);
}
}