【问题标题】:JSON Java - Unexpected character (a) at position 59JSON Java - 位置 59 的意外字符 (a)
【发布时间】:2017-05-09 10:33:07
【问题描述】:

我正在尝试解析以下 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)

请帮帮我。提前谢谢你。

【问题讨论】:

  • "{"auth" 替换为"auth"
  • 不是一个有效的 json。尝试jsonlint.com 进行验证。

标签: java json parsing


【解决方案1】:

您可以使用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"
]
}

【讨论】:

    【解决方案2】:

    您的 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 中删除 "。

    【讨论】:

      【解决方案3】:

      使用 Json 验证器来验证字符串 并使用诸如 Gson 或 Jackson 之类的 json 解析器库,它会自动完成您的大量工作,并且您无需编写太多代码即可解析

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-04
        • 2015-03-23
        • 2021-12-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多