为什么JSON Parser正在给出,错误org.json.JSONException:期望':'一把钥匙在5 [字符6第1行]之后

时间:2016-04-09 12:04:13

标签: java json

我是json的新手,我必须从Web服务响应中获取值。我已经使用了org.json库.Below是示例json值:

{"tms_guid": "9LaHmoHpmTd811R",
   "recharge_status": "100",
   "message": "Transaction Successful",
   "response_time": {
      "verifyClient": 0.0281,
      "verifyGuid": 0.8695,
      "verifyOperator": 0.8698,
      "verifyMsid": 0.8698,
      "tms_guid": 1.6971,
      "queryErr": 7.4243,
      "StoringRecharge": 7.4358,
      "UpdatingBalance": 7.448
   }
}

我的解析JSON输入字符串是:

private final static String JSON_TEST_DATA
        = "{"
        + "   \"tms_guid\": \"9LaHmoHpmTd811R\", "
        + "   \"recharge_status\": \"100\", "
        + "   \"message\": \"Transaction Successful\", "
        + "   \"response_time\": { "
        + "      \"verifyClient\": 0.0281, "
        + "      \"verifyGuid\": 0.8695, "
        + "      \"verifyOperator\": 0.8698,"
        + "      \"verifyMsid\": 0.8698,"
        + "      \"tms_guid\": 1.6971,"
        + "      \"queryErr\": 7.4243,"
        + "      \"StoringRecharge\": 7.4358,"
        + "      \"UpdatingBalance\": 7.448"
        + "   }"
        + "}";

public static void main(final String [] argv)抛出JSONException {

    System.out.println(JSON_TEST_DATA);
    final JSONObject testObj = new JSONObject(JSON_TEST_DATA);
 System.out.println(testObj.toString());

}

例外情况如下:

Exception in thread "main" org.json.JSONException: Expected a ':' after a key at 5 [character 6 line 1]
    at org.json.JSONTokener.syntaxError(JSONTokener.java:432)
    at org.json.JSONObject.<init>(JSONObject.java:206)
    at org.json.JSONObject.<init>(JSONObject.java:310)
    at com.kalsym.wsp.sp.icebeep.TestIceBeep.main(TestIceBeep.java:73)

我见过类似的post。但无法想象解决方案。

1 个答案:

答案 0 :(得分:1)

我没有看到任何问题。我使用了相同的代码,但我能够执行你的程序:

你可以看到我的代码,我也使用过相同的java api。

import org.json.JSONException;
import org.json.JSONObject;

public class TestCode {

    private final static String JSON_TEST_DATA
    = "{"
    + "   \"tms_guid\": \"9LaHmoHpmTd811R\", "
    + "   \"recharge_status\": \"100\", "
    + "   \"message\": \"Transaction Successful\", "
    + "   \"response_time\": { "
    + "      \"verifyClient\": 0.0281, "
    + "      \"verifyGuid\": 0.8695, "
    + "      \"verifyOperator\": 0.8698,"
    + "      \"verifyMsid\": 0.8698,"
    + "      \"tms_guid\": 1.6971,"
    + "      \"queryErr\": 7.4243,"
    + "      \"StoringRecharge\": 7.4358,"
    + "      \"UpdatingBalance\": 7.448"
    + "   }"
    + "}";

    public static void main (String arg[]) throws JSONException{

        //System.out.println(JSON_TEST_DATA);
        final JSONObject testObj = new JSONObject(JSON_TEST_DATA);

        System.out.println(" --"+testObj.getString("recharge_status")+"\n");
    System.out.println(testObj.toString());
    }

}

可能是一些问题。