Pranjal Choladhara
Pranjal Choladhara

Reputation: 913

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

I am creating JSON using jsp for REST url. I get the output in my browser fine. My JSON output is something like this:

{"SUCCESS":1,
"CLIENTS":
  [{"COMPANY":"BOOK PALACE, PANBAZAR",
     "C_NAME":"",
     "ID":"3",
     "EMAIL":"[email protected]",
     "CODE":0}
  ]
 }

When I call the url, and try to parse the output from a url in my swing application, it gives me error. I am using the following codes to parse the 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;

    }

Why this gives me error in:

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

Is my JSON ok? Am I doing anything wrong in parsing? I've created the json using jsp.

Upvotes: 0

Views: 587

Answers (1)

wero
wero

Reputation: 33000

Most likely the response did not contain the expected JSON.

A possible cause is an error during request processing which caused the server to return an error response, for instance an error HTML page. You can check this by inspecting the value of String json before you try to parse it.

To avoid this you should always check the HTTP status code of the response:

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

Upvotes: 1

Related Questions