There is a need to change the content of the php code in which the JSON response is changed. It seems the difference is only in quotes, and displays an error. The PHP code at which it turns out to output successfully:

<?php // серверная часть вывода json $login = $_POST['login']; $pass = $_POST['pass']; if($login == "user" & $pass == "pass") { ?> { "data":[ { "name":"Lobs", "reit":"110", "balance":"99" } ] }<?php } ?> 

The PHP code under which the error appears

Error parsing data org.json.JSONException: Value {"balance": 99, "reit": 110, "name": "Lobs"} at data of type org.json.JSONObject cannot be converted to JSONArray:

 <?php // серверная часть вывода json $login = $_POST['login']; $pass = $_POST['pass']; if($login == "user" & $pass == "pass") { ?> { "data":[ { "name":"Lobs", "reit":110, "balance":99 } ] }<?php } ?> 

How I try to display:

 public void JSONURL(String result) { textView20 = (TextView) findViewById(R.id.textView20); textView34 = (TextView) findViewById(R.id.textView34); textView60 = (TextView) findViewById(R.id.textView60); try { //создали читателя json объектов и отдали ему строку - result JSONObject json = new JSONObject(result); //дальше находим вход в наш json им является ключевое слово data JSONArray urls = json.getJSONArray("data"); //проходим циклом по всем нашим параметрам for (int i = 0; i < urls.length(); i++) { HashMap<String, Object> hm; hm = new HashMap<String, Object>(); //читаем что в себе хранит параметр balance hm.put(NAME, urls.getJSONObject(i).getString("name").toString()); //читаем что в себе хранит параметр reit hm.put(REIT, urls.getJSONObject(i).getString("reit").toString()); //читаем что в себе хранит параметр balance hm.put(BALANCE, urls.getJSONObject(i).getString("balance").toString()); myBooks.add(hm); //выводим textView20.setText((CharSequence) hm.get("name")); textView34.setText((CharSequence) hm.get("reit")); textView60.setText((CharSequence) hm.get("balance")); } } catch (JSONException e) { Log.e("log_tag", "Error parsing data " + e.toString()); } 

I know that there is an extra code that is not written correctly ... What needs to be changed? Already tried everything ... (((Note that I am a novice programmer;). Thanks in advance.

  • it seems to me that, besides the quotes, you have somewhere lost even square brackets []. output to the log before using it - Vladyslav Matviienko
  • Absolutely right! I checked the result and there really are no square brackets: {"data": {"name": "Lobs", "reit": 110, "balance": 99}}. What do you advise to do? Do I have to redo PCPs? - Lobs
  • As if, if there should be an array, but not an object, then it is necessary. If there is only one object, just change the code so that it does not expect an array, but an object. - Vladyslav Matviienko
  • Yes Yes Yes!! Thank!! :))))))) - Lobs

1 answer 1

besides the quotes, you have also lost some square brackets [] . As a result, you have a JSONArray in place of a JSONObject . output to the log before using it