2017-05-09 7 views
0

次のJSONを解析しようとしています。しかし、例外を得る助けてください。ここでJSON Java - 位置59の予期しない文字(a)

{ 
    "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" '' 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形式を確認することができます。

https://jsonformatter.curiousconcept.com/

Javaコードはこれを好きなはずです、

を。この、JSONObjectの前後

{ 
    "method": "demo", 
    "clazz": "storage", 
    "params": [ 
    "", 
    "LOGIN", 
    {"auth": {"tenantName": "AUTH_Tonny", "casauth": {"username": "Tonny", "tgt": "TGT-1876hkahaadcaweyfiowufssadsfsdf"}}}, 
    "http://ipstorage.google.com" 
    ] 
} 

ありなし」のようなJSONをしたいです

... 
String pp = "[\"" + tokenId + "\",\"" + message + "\"," 
        + authentication + ",\"" + url1 + "\"]"; 
... 

"をJSONで削除してください。

0

が文字列 を検証し、そのようなGsonやジャクソンなどJSONパーサーライブラリを使用してJSONのバリデータを使用して、「`それはあなたの仕事の多くを自動化するだろうとあなたは

関連する問題