我的网站将一个JWT (内置于php)发送到用Java开发的My。
JWT在名为DATI的自定义字段中包含一个JSON字符串。为了去除DATI字段中包含的字符串,我使用了JJWT库:
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}]我在“STRING_JWT”中测试了https://jwt.io/,在这里我得到了正确的引号:
{
"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字符串。
发布于 2018-09-14 12:55:39
这可能会有帮助
您已经有了包含所需声明的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字符串(带有引号)。
https://stackoverflow.com/questions/52318182
复制相似问题