【问题标题】:Converting base64 token after decoding to jsonString解码后将base64令牌转换为jsonString
【发布时间】:2020-03-11 05:29:04
【问题描述】:

我正在尝试将 json 字符串转换为我各自的 pojo 类。 json 字符串是 base64 令牌的解码版本。
解码后的值为

{"userId":"1234567890","userName": "John Doe", roles: ["admin","users"]}

我正在使用代码String jsonFormat=objectMapper.writeValueAsString(decoded);,其中解码是上面提到的字符串。

当我尝试使用 objectmapper 将其转换为对象时遇到错误

    objectMapper.readValue(jsonFormat, PtsbUser.class);

错误:

    com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `com.ptsb.rbaccomponent.models.PtsbUser` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('{userId:"1234567890",userName: "John Doe", roles: ["admin","users"]}')
     at [Source: (String)""{userId:\"1234567890\",userName: \"John Doe\", roles: [\"admin\",\"users\"]}\n     \n  \n    ""; line: 1, column: 1]

我已经更新了解码后的字符串,也将括号放在键中。我正在使用https://www.base64encode.org/ 编码base64,它给了我令牌

eyJ1c2VySWQiOiIxMjM0NTY3ODkwIiwidXNlck5hbWUiOiAiSm9obiBEb2UiLCByb2xlczogWyJhZG1pbiIsInVzZXJzIl19CiAgICAgCiAgCiAgICA=

我正在使用下面的 util 代码来获取我的解码字符串:

    Base64.getDecoder().decode(encoded);
            String decripted=new String (decoded);
            return decripted

【问题讨论】:

  • 能否在您的问题中添加您的 PtsbUser.class?
  • 另外,你最好分享你编码的 base64 令牌
  • @MOnkey 我更新了我的问题。如果您需要任何进一步的信息,请告诉我

标签: java json objectmapper


【解决方案1】:

如果数据是 bas64 编码的 json 字符串,你可以这样做:

objectMapper.readValue(Base64.getDecoder().decode(data),  PtsbUser.class);

但您的原始字符串无效 json:

{userId:"1234567890",userName: "John Doe", roles: ["admin","users"]}  

这是一个有效的:

{"userId":"1234567890","userName": "John Doe", "roles": ["admin","users"]}

objectMapper.writeValueAsString 可用于将任何 Java 值序列化为 String 的方法

objectMapper.writeValueAsString(decoded); 这里不需要 因为此方法用于java -> json 序列化,并且您已经有要反序列化的 json 字符串

【讨论】:

    【解决方案2】:

    您的解码字符串应该是这样的,以便将其转换回您的 java 对象(即PtsbUser)。

    "{\"userId\":\"1234567890\",\"userName\": \"John Doe\", \"roles\": [\"admin\",\"users\"]}";
    

    但在这里,由于错误,我可以看到您解码的 json 字符串是

    "{userId:\"1234567890\",userName: \"John Doe\", roles: [\"admin\",\"users\"]}\n     \n  \n    "";
    

    在这里,我不确定你是如何解码你的 json 字符串的,一旦你的 json 字符串如上所示被正确解码,你可以使用以下代码将其转换为 PtsbUser 对象,如下所示,

     String decoded = "{\"userId\":\"1234567890\",\"userName\": \"John Doe\", \"roles\": [\"admin\",\"users\"]}";
     ObjectMapper objectMapper = new ObjectMapper();
     PtsbUser ptsbUser = objectMapper.readValue(decoded, PtsbUser.class);
     System.out.println(ptsbUser);
    

    我也希望你的 PtsbUser.java 课程看起来像这样。

    public class PtsbUser {
    
        String userId;
        String userName;
        List<String> roles;
        public String getUserId() {
            return userId;
        }
        public void setUserId(String userId) {
            this.userId = userId;
        }
        public String getUserName() {
            return userName;
        }
        public void setUserName(String userName) {
            this.userName = userName;
        }
        public List<String> getRoles() {
            return roles;
        }
        public void setRoles(List<String> roles) {
            this.roles = roles;
        }
    }
    

    这是解码字符串的方法

    String inputToDecode = "eyJ1c2VySWQiOiIxMjM0NTY3ODkwIiwidXNlck5hbWUiOiAiSm9obiBEb2UiLCByb2xlczogWyJhZG1pbiIsInVzZXJzIl19CiAgICAgCiAgCiAgICA=";
    Decoder decoder = Base64.getDecoder();
    String decodedString = new String(decoder.decode(inputToDecode ), "UTF-8");
    return decodedString;
    

    【讨论】:

    • 是的,我只是这样使用,但我创建了一个实用程序来使用 byte[] decoded=Base64.getDecoder().decode(encoded); String decrited=new String (已解码);返回已解密;给我解码的字符串
    • @unknown,如果对您有帮助,您能接受答案吗?
    【解决方案3】:

    您将不需要此行,因为您已经将它作为字符串。

      String jsonFormat = objectMapper.writeValueAsString(decoded);
    

    这将在您解码的字符串周围加上双引号。此方法仅在您将字符串写入输出/响应时使用。

    可以直接在readValue方法中使用解码后的字符串

    objectMapper.readValue(decoded, PtsbUser.class);
    

    你的 JSON 有问题。您可以在 https://jsonlint.com/

    验证您的 JSON

    在这里,您需要用双引号将您的密钥括起来以使其成为有效的 JSON

    {
      "userId": "1234567890",
      "userName": "John Doe",
      "roles": ["admin", "users"] 
    }
    

    【讨论】:

      猜你喜欢
      • 2019-10-17
      • 2012-08-03
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      • 2020-10-11
      • 2023-03-08
      • 1970-01-01
      相关资源
      最近更新 更多