I'm getting W/System.err: org.json.JSONException: Value when running this code to get the JSON. However, when I use:

[{"name":"Abhishek","password":"123","contact":"1111111111","country":"India"},{"name":"Rahul","password":"1s","contact":"1sdfsdf","country":"India"},{"name":"Abhishek","password":"aar","contact":"asdbsfg","country":"India"}]

(https://api.myjson.com/bins/j5f6b), a tester JSON URL, it gives me the output.

I have tried changing it to JSON Object in places but that isn't helping.

protected Void doInBackground(Void... voids) {
    try {
    URL url = new URL("http://www.free-map.org.uk/fm/ws/bsvr.php? 
    bbox=-0.73,51.04,-0.71,51.06&way=highway&format=json");
    HttpURLConnection httpURLConnection = (HttpURLConnection) 
    url.openConnection();
    InputStream inputStream = httpURLConnection.getInputStream();
    BufferedReader bufferedReader = new BufferedReader(new 
    InputStreamReader(inputStream));
    String line = "";
    while(line != null){
    line = bufferedReader.readLine();
    data = data + line;
    }

    JSONArray JA = new JSONArray(data);
    for(int i =0 ;i <JA.length(); i++){
    JSONObject JO = (JSONObject) JA.get(i);
    singleParsed = "name:" + JO.get("name") + "\n";

    dataParsed = dataParsed + singleParsed +**"\n"** ;

    }

    } catch (MalformedURLException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } catch (JSONException e) {
    e.printStackTrace();
    }

    return null;
}

protected void onPostExecute(Void aVoid) {
    super.onPostExecute(aVoid);

    twod.data.setText(this.dataParsed);

}

I expect a name output and am currently getting nothing but W/System.err: org.json.JSONException:

1 Answers

0
Virag Brahme On

The problem seems to be with your parsing logic.

Check this:

JSONObject jsonObject = new JSONObject(data);
JSONArray JA = jsonObject.getJSONArray("features");
for (int i = 0; i < JA.length(); i++) {
    JSONObject JO = (JSONObject) JA.get(i);
    System.out.print(JO);
}

You have to create a JSONObject first, later extract the JSONArray from it.