2017-05-09 19 views
0

我想解析下面的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) 

請幫助我。先謝謝你。

+3

替換'」 {「auth」'with'「auth」'。 –

+4

不是有效的json,請嘗試https://jsonlint.com/來驗證。 – Devavrata

回答

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" 
] 
} 
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。

0

使用JSON驗證驗證字符串 並使用JSON解析器類庫,如GSON或傑克遜,它會自動很多你的工作,你不需要太多的代碼解析

相關問題