解析数据data时出错.JSONException:JSONObject文本必须以' {'在1 [字符2行1]

时间:2015-09-29 11:33:32

标签: java json swing

我正在使用jsp for REST url创建JSON。我在浏览器中得到的输出很好。我的JSON输出是这样的:

{"SUCCESS":1,
"CLIENTS":
  [{"COMPANY":"BOOK PALACE, PANBAZAR",
     "C_NAME":"",
     "ID":"3",
     "EMAIL":"pranjalcholadhara662@gmail.com",
     "CODE":0}
  ]
 }

当我调用url并尝试解析swing应用程序中url的输出时,它会给我错误。我使用以下代码来解析JSON。

List<NameValuePair> params = new ArrayList<>();
           params.add(new BasicNameValuePair("EMAIL", mail)) ;
            JSONObject json = jParser.makeHttpRequest(YOURUrl, "GET", params);
            System.out.println(json.toString());
             try {
                int success = json.getInt("SUCCESS");                

                if (success == 1) {
                    products = json.getJSONArray("CLIENTS");  
                    clients = json.getJSONObject("CLIENTS");
                    for (int i = 0; i < products.length(); i++) {
                        JSONObject c = products.getJSONObject(i);

                        String Company = c.getString("COMPANY");
                        String Name = c.getString("C_NAME");
                        String Email = c.getString("EMAIL");
                        String Code = c.getString("CODE");
                        String ValidTill = c.getString("VALID_TILL");
                        String sl = c.getString("ID");
                        COMPANY = Company;
                        CNAME = Name;
                        EMAIL = Email;
                        CODE = Code;
                        VALID_TILL = ValidTill;
                        SL = sl;
                     }
                } 
            } catch (JSONException e) {
                System.err.println(e);
            }


public JSONObject makeHttpRequest(String url, String method,
            List<NameValuePair> params) {

        // Making HTTP request
        try {
            // check for request method
            if(method.equals("POST")){
                // request method is POST
                // defaultHttpClient
                HttpClient httpClient = HttpClientBuilder.create().build();
                HttpPost httpPost = new HttpPost(url);
                httpPost.setEntity(new UrlEncodedFormEntity(params, "utf-8"));

                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();

            }else if(method.equals("GET")){
                // request method is GET
                HttpClient httpClient = HttpClientBuilder.create().build();
                String paramString = URLEncodedUtils.format(params, "utf-8");
                url += "?" + paramString;
                HttpGet httpGet = new HttpGet(url);

                HttpResponse httpResponse = httpClient.execute(httpGet);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();
            }           

        } catch (UnsupportedEncodingException | ClientProtocolException e) {
            System.out.println(e);
        } catch (IOException e) {

        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "utf-8"), 8);
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line).append("\n");
            }
            is.close();
            json = sb.toString();
        } catch (Exception e) {
            System.out.println("Error converting result " + e.toString());
        }

        // try parse the string to a JSON object
        try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            System.out.println("Error parsing data " + e.toString());
        }

        // return JSON String
        return jObj;

    }

为什么这会给我带来错误:

Error parsing data data.JSONException: A JSONObject text must begin with '{' at 1 [character 2 line 1]

我的JSON好吗?我在解析时做错了吗?我使用jsp创建了json。

1 个答案:

答案 0 :(得分:1)

很可能响应不包含预期的JSON。

可能的原因是请求处理期间发生错误,导致服务器返回错误响应,例如错误的HTML页面。您可以在尝试解析之前检查String json的值来检查这一点。

为避免这种情况,您应该始终检查响应的HTTP状态代码:

 if (httpResponse.getStatusLine().getStatusCode() == 200)
     ... // extract JSON from response
 else
     ... // error