1

I'm parsing a json file but i get this message: org.json.JSONException: End of input at character 0 of the contetnn of the file is:

2
  • post relative json parsing code then we will to help u more Commented Aug 17, 2012 at 17:43
  • End of input at character 0 of Of what? You're missing the part of the error that'll tell us where the problem is. I'm expecting it to say something like "of line ____". Commented Aug 17, 2012 at 17:55

2 Answers 2

1

Are you sure there is not an empty line at the end of the file? Like this:

[
{
code: "UNLC",
cours_jour: "40 020",
variation: "0.00"
},
{
code: "UNXC",
cours_jour: "7 450",
variation: "0.00"
}
]
<-- Empty line here!
Sign up to request clarification or add additional context in comments.

Comments

0

Your JSON Object fields need to be encapsulated by quotes

IE

{
"code": "BOAC",
"cours_jour": "29 000",
"variation": "-1.69"
}

How was the JSON file generated?

--Edit

You can use the below code to download the page to a string and then convert it to a JSONArray and then pull each JSONObject. You cannot run any web requests on the main thread so either extend a new asynctask or thread or runnable to perform the below

DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpPost httpost = new HttpPost("http://www.impaxis-securities.com/securities/cours-actions/cours.json");
        httpost.setHeader("Accept", "application/json");
        httpost.setHeader("Content-type", "application/json");
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String response;
        try {
                response = httpclient.execute(httpost, responseHandler);
                JSONArray arr = new JSONArray(response);
                int arrLength = arr.length();
                    if(arrLength > 0)
                    {
                        for(int i = 0; i < arrLength; i++)
                        {
                            JSONObject item = arr.getJSONObject(i);
                            String code = item.getString("code");
                            String cours_jour = item.getString("cours_jour");
                            String variation = item.getString("variation");
                            //Either insert to a DB or add to an array list and return it
                        }
                    }
                } 
        catch (ClientProtocolException e) {
            //Issue with web server 
            } 
        catch (IOException e) {
            //Issue with request
            } 
        catch (JSONException e) {
            //ISSUE Parsing JSON from site
        }

---Edit

I tested the code and it looks like there is a bug with the JSON plugin/REST service

http://drupal.org/node/1433436

4 Comments

the json file was generated by a drupal module.. i don't really know the name of the module but i work with the json file they give to me..
Sounds like the array object created by the module isn't putting quotes around it's keys..IE $a = array("code" => "$code", "cours_jour" => "$cours_jour", "variation" => "$variation"); <br /> json_encode($a); but instead they're doing $a = array(code => "$code", cours_jour => "$cours_jour", variation => "$variation");
hi. Do you really get the code, cours_jours and variation.?? i do System.out.println(response) int the try catch but i have nothing
Did you read about the bug in the link drupal.org/node/1433436 -- try switching the mode as described in the link and then try my code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.