简体   繁体   English

Marshal一个java.util.Map <String,String>

[英]Marshal a java.util.Map<String,String>

The next question for my restful JSON Service. 我的宁静JSON服务的下一个问题。

import java.util.Map;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

/**
 * @author Martin Burchard
 * 
 */
@XmlRootElement(name = "user")
@XmlAccessorType(XmlAccessType.FIELD)
public class User {
    private String id;
    private String nickname;
    private String email;
    private String password;
    private Map<String, String> user_attributes;

}

Currently the service delivers the following JSON (indented for better reading): 目前,该服务提供以下JSON(缩进以便更好地阅读):

{
    "user" : {
        "id" : "9bdf40ea-6d25-4bc3-94ad-4a3d38d2c3ca",
        "email" : "test.user@test.de",
        "password" : "xXpd9Pl-1pFBFuX9E0hAYGSDTyJQPYkOtXGvRCrEtMM",
        "user_attributes" : {
            "entry" : [{
                    "key" : "num",
                    "value" : 123
                }, {
                    "key" : "type",
                    "value" : "nix"
                }
            ]
        }
    }
}

The funny think is, internally the num 123 is a java.lang.String... 有趣的想法是,内部数字123是java.lang.String ...

I don't understand what is explained here http://cxf.apache.org/docs/jax-rs-data-bindings.html#JAX-RSDataBindings-DealingwithJSONarrayserializationissues 我不明白这里解释了什么http://cxf.apache.org/docs/jax-rs-data-bindings.html#JAX-RSDataBindings-DealingwithJSONarrayserializationissues

I like to have this JSON: 我喜欢这个JSON:

{
    "user" : {
        "id" : "9bdf40ea-6d25-4bc3-94ad-4a3d38d2c3ca",
        "email" : "test.user@test.de",
        "password" : "xXpd9Pl-1pFBFuX9E0hAYGSDTyJQPYkOtXGvRCrEtMM",
        "user_attributes" : {
            "num" : "123",
            "type" : "nix"
        }
    }
}

I changed the JSON provider to Jackson. 我将JSON提供程序更改为Jackson。 Now my JSON looks like I like it... 现在我的JSON看起来像我喜欢它...

The only thing that comes to my mind is to use JAXB XmlAdapter. 我唯一想到的就是使用JAXB XmlAdapter。 You can define how a given object (in your case Map) would be mapped to JSON string. 您可以定义给定对象(在您的情况下为Map)如何映射到JSON字符串。

使用像杰克逊这样的JSON库

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.String无法强制转换为java.util.Map - java.lang.String cannot be cast to java.util.Map 阅读java.util.Map <String,?> 来自文件 - read java.util.Map<String,?> from a file 如何将json解组到java.util.Map <String, Object> 与MOXy? - How to unmarshal json to java.util.Map<String, Object> with MOXy? Scala 2.8.1隐式转换为java.util.List <java.util.Map<String, Object> &gt; - Scala 2.8.1 implicitly convert to java.util.List<java.util.Map<String, Object>> 预期的数组类型; 找到:'java.util.map<java.lang.string,java.lang.string> '</java.lang.string,java.lang.string> - Array type expected; found: 'java.util.map<java.lang.String,java.lang.String>' 将java.util.Map [String,Object]转换为scala.collection.immutable.Map [String,Any] - convert java.util.Map[String, Object] to scala.collection.immutable.Map[String, Any] 在scala类中使用类型为java.util.Map [String,String]的arg覆盖java方法 - Overriding java method with arg of type java.util.Map[String, String] in scala class 一个 java 方法返回一个 java.util.Map 在 openapi 中生成一个字符串返回,而不是一个 Z46BZ1EA056CAABEEA23F - A java method returning a java.util.Map generates a String return in openapi instead of a Map java.lang.ClassCastException:java.lang.String 无法转换为 java.util.Map - java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Map 无法将java.lang.String强制转换为java.util.Map $ Entry,并且分页缺少必需的属性 - java.lang.String cannot be cast to java.util.Map$Entry and pagination missing required attribute
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM