“不允许的键字符"; json输出错误 [英] "Disallowed Key Characters" error in json output

查看:93
本文介绍了“不允许的键字符"; json输出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试执行API并尝试获取json响应,但是对于bf.readLine(),我却收到了"Disallowed Key Characters"的错误.

I'm trying to execute API and try to get json response, but I'm getting error as "Disallowed Key Characters" for bf.readLine().

以下是我尝试使用的代码.但是当我在Web浏览器中运行请求url时,我得到的响应没有问题,但是通过使用Java代码,我无法提取数据.请帮助

Following is the code that I'm trying to use. But when I run the request url in web browser I'm getting response without issue.But by using java code I'm unable to extract data. Please help

String uri = "http://192.168.77.6/Ivr_ABN_API/?id?id="
            + mobile;
    URL url;
    Gson json = null;
    try {
        url = new URL(uri);
        json = new Gson();

        HttpURLConnection connection;
        access_token = db.getAccessTokenFromDB();
        connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");

        System.out.println("URL:" + uri);

        connection.setRequestProperty("Content-Type", "application/json");
        int status = connection.getResponseCode();
        resCode = Integer.toString(status);


                InputStream in = connection.getInputStream();
                BufferedReader bf = new BufferedReader(
                        new InputStreamReader(in));
                System.out.println("bf.readLine() - "+bf.readLine());

                while ((output = bf.readLine()) != null) {
                    JSONObject obj = new JSONObject(output);
                    System.out.println("output is "+output);
                    resCode = obj.getString("resCode");
                    resDesc = obj.getString("COUNT");

                }


        connection.disconnect();

推荐答案

尝试一下

BufferedReader in = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));

代替

 BufferedReader bf = new BufferedReader(new InputStreamReader(in));

因为您需要添加一种编码技术,通过该技术,BufferedReader能够以正确的格式编码任何特殊字符.

because you need to add encoding technique by which a BufferedReader able to encode any special character in proper format.

希望这对您有所帮助.

谢谢.

这篇关于“不允许的键字符"; json输出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆