JJWT解密一个JSON字符串去掉引号-Java

Decripting by JJWT a JSON String removed quotation marks -Java

我的网站将 JWT(内置于 php)发送到我在 Java 中开发的应用程序。

JWT 在名为 DATI 的自定义字段中包含一个 JSON 字符串。 我使用库 JJWT 来描述 DATI 字段中包含的字符串:

Claims MY_CLAIMS = Jwts.parser().setSigningKey(SECRET_KEY__Byte).parseClaimsJws(STRING_JWT).getBody(); 
ArrayList ARRAY = MY_CLAIMS .get("DATI", ArrayList.class);
String DECODED_STRING_INSIDE_DATI =String.valueOf(ARRAY);

我以正确的方式得到字符串 "DECODED_STRING_INSIDE_DATI"(即 JSON 字符串),但由于某些原因,引号 (") 被删除了:

[{id=3, id_rivenditore=-1, id_cliente=-1, ip_address=192.168.1.6, nome=DonalDuck, note=ByBye, enabled=1}]

我在“https://jwt.io/”中测试了STRING_JWT,我正确地得到了引号:

{
  "iss": "www.mySite.it",
  "exp": 1536913435,
  "sub": "WebApp",
  "DATI": [
    {
      "id": "3",
      "id_rivenditore": "-1",
      "id_cliente": "-1",
      "ip_address": "192.168.1.6",
      "nome": "DonalDuck",
      "note": "ByBye",
      "enabled": "1"
    }
  ]
}

我真的不知道怎么解决,因为我无法正确读取JSON字符串。我使用 jackson 库读取 Json String

这可能有帮助,

您已经 ArrayList 包含所需的声明,

ArrayList ARRAY = MY_CLAIMS.get("DATI", ArrayList.class);

要获得此 ArrayList 中包含的 JSON 声明字符串,请尝试以下代码。

ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectMapper mapper = new ObjectMapper();
mapper.writeValue(out, ARRAY);
byte[] data = out.toByteArray();
String str = new String(data);

str 包含格式正确的 JSON 字符串(带引号)。