0

我的网站将 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 字符串。我使用杰克逊库来读取 Json 字符串

4

1 回答 1

1

这可能会有所帮助,

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

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

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

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

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

于 2018-09-14T12:55:39.893 回答