我正在尝试解析以下JSON。但是获得例外请帮助。
{
"method": "demo",
"clazz": "storage",
"params": [
"",
"LOGIN",
"{"auth": {"tenantName": "AUTH_Tonny", "casauth": {"username": "Tonny", "tgt": "TGT-1876hkahaadcaweyfiowufssadsfsdf"}}}",
"http://ipstorage.google.com"
]
}
这是Java代码:
String tokenId = "";
String message = "LOGIN";
String url1 = "http://ipstorage.google.com";
String authentication = "{\"auth\": {\"tenantName\": \"AUTH_" + test2.getUsername()
+ "\", \"casauth\": {\"username\": \""
+ test2.getUsername() + "\", \"tgt\": \""
+ test2.getPassword() + "\"}}}";
String pp = "[\"" + tokenId + "\",\"" + message + "\",\""
+ authentication + "\",\"" + url1 + "\"]";
String msg1 = "{\"method\":\"demo\",\"clazz\":\"storage\",\"params\":" + pp + "}";
System.out.println(msg1);
JSONObject jo = (JSONObject) new JSONParser().parse(msg1);
System.out.println("##");
System.out.println(jo);
我得到的输出和例外是:
{"method":"demo","clazz":"storage","params":["","LOGIN","{"auth":
{"tenantName": "AUTH_Tonny", "casauth": {"username": "Tonny", "tgt": "TGT -
1876 hkahaadcaweyfiowufssadsfsdf"}}}","http://ipstorage.google.com"]}
Exception in thread "main" Unexpected character (a) at position 59.
at org.json.simple.parser.Yylex.yylex(Unknown Source)
at org.json.simple.parser.JSONParser.nextToken(Unknown Source)
at org.json.simple.parser.JSONParser.parse(Unknown Source)
at org.json.simple.parser.JSONParser.parse(Unknown Source)
at org.json.simple.parser.JSONParser.parse(Unknown Source)
at com.t.g.i.e.utils.Test.main(Test.java:74)
请帮帮我。提前谢谢。
答案 0 :(得分:0)
您可以使用http://www.jsoneditoronline.org/来验证您的json文件。
{
"method": "demo",
"clazz": "storage",
"params": [
"",
"LOGIN",
{
"auth": {
"tenantName": "AUTH_Tonny",
"casauth": {
"username": "Tonny",
"tgt": "TGT-1876hkahaadcaweyfiowufssadsfsdf"
}
}
},
"http://ipstorage.google.com"
]
}
答案 1 :(得分:0)
您的JSON格式不对。也许你想要这样的JSON,
{
"method": "demo",
"clazz": "storage",
"params": [
"",
"LOGIN",
{"auth": {"tenantName": "AUTH_Tonny", "casauth": {"username": "Tonny", "tgt": "TGT-1876hkahaadcaweyfiowufssadsfsdf"}}},
"http://ipstorage.google.com"
]
}
没有“JSONObject之前和之后。您可以使用此工具检查您的JSON格式。
https://jsonformatter.curiousconcept.com/
你的java代码应该是这样的,
...
String pp = "[\"" + tokenId + "\",\"" + message + "\","
+ authentication + ",\"" + url1 + "\"]";
...
删除您的JSON。
答案 2 :(得分:0)
使用Json验证器验证字符串 并使用像Gson或Jackson这样的json解析器库,它可以自动完成你的很多工作而且你不需要编写很多代码来解析