我想解析下面的JSON。但獲得例外,請幫助。JSON Java - 意外的字符(a)在位置59
{
"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)
請幫助我。先謝謝你。
替換'」 {「auth」'with'「auth」'。 –
不是有效的json,請嘗試https://jsonlint.com/來驗證。 – Devavrata