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 Answers
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
4 Comments
Hoshouns
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..
ainesophaur
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");
Hoshouns
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
ainesophaur
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
End of input at character 0 ofOf 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 ____".