Anton Kashpor Anton Kashpor - 2 months ago 13
Android Question

Value of type java.lang.String cannot be converted to JSONArray

I've spent 2 days to find a solution with of problem.

Here is the error:

E/log_tag: Error parsing data org.json.JSONException: Value of type java.lang.String cannot be converted to JSONArray


Here is JSON:

[
{
"Id": "5207fc6473516724343ce7a5",
"Name": "Эриван",
"Types": [
"Ресторан"
],
"Latitude": 53.904752,
"Longitude": 27.521095,
"OperatingTime": [
{
"Day": 1,
"Start": "10:00:00",
"Finish": "23:00:00"
},
{
"Day": 2,
"Start": "10:00:00",
"Finish": "23:00:00"
},
{
"Day": 3,
"Start": "10:00:00",
"Finish": "23:00:00"
},
{
"Day": 4,
"Start": "10:00:00",
"Finish": "23:00:00"
},
{
"Day": 5,
"Start": "10:00:00",
"Finish": "23:00:00"
},
{
"Day": 6,
"Start": "08:00:00",
"Finish": "23:00:00"
},
{
"Day": 0,
"Start": "08:00:00",
"Finish": "23:00:00"
}
],
"IsBookingAvailable": false
}]


Class for getting String value:

public class JSONGet {

public static String getJSONfromURL(String url){
InputStream is = null;
String result = "";
JSONArray jArray = null;

// Download JSON data from URL
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();

}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}

// Convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}

try{

jArray = new JSONArray(result);
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}

return result;
}


}

And here is converting to JSONArray:

String jsonObjRecv = JSONGet.getJSONfromURL(URL_LIST);

JSONArray jsonArr = new JSONArray(jsonObjRecv);


I was trying to get Json object then convert it into Json array but I received the same error.

Answer

The problem is that your JSON is in the incorrect format. I have tried with your sample JSON and found the solution to it. Now the inbuilt JSONObject and JSONArray cannot be used to get such a json response.

You need to add json-simple library to your project by adding it to gradle:

compile group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1'

Or download the library "json-simple-1.1.1.jar" from this link https://json-simple.googlecode.com/files/json-simple-1.1.1.jar.

Then you can parse your JSON easily and it won't give any error. I have made a small sample code for you on how to use it :

import org.json.simple.JSONArray;
import org.json.simple.parser.JSONParser;

JSONParser parser_obj = new JSONParser();
JSONArray array_obj = (JSONArray) parser_obj.parse("String from web service"); 
// in your case it will be "result" 

Then you can process it as per your need.

Comments