Hi, I have php, which json'om returns to me several values:

  • day
  • class
  • letter
  • lesson
  • task

It takes them from the SQL table and returns sort of like an array.

Here is the php code:

$sql = "SELECT * FROM `table_name`"; $result = $conn->query($sql); while ($row = $result->fetch_assoc()) { $day["day:"] = ($row['day']); $class["class:"] = ($row['class']); $letter["letter:"] = ($row['letter']); $lesson["lesson:"] = ($row['lesson']); $task["task:"] = ($row['task']); echo json_encode($day); echo json_encode($class); echo json_encode($letter); echo json_encode($lesson); echo json_encode($task); } 

So I try to accept it in java code, and I seem to accept it, and everything would be okay, but then I encounter a problem ..

I need to stuff the data into a ListView ..

This script gives me such a thing:

 {"day:":"Четверг"}{"class:":"3 класс"}{"letter:":"А"}{"lesson:":"География"}{"task:":"Задание 1"} 

I took it in the application to the string and passed it to the JSONObject and try to pass to the ListView with the following code:

 public void JSONURL(String result) { try { JSONObject json = new JSONObject(result); System.out.println(json); JSONObject uDay = json.getJSONObject("day"); JSONObject uClass = json.getJSONObject("class"); JSONObject uLetter = json.getJSONObject("letter"); JSONObject uLesson = json.getJSONObject("lesson"); JSONObject uTask = json.getJSONObject("task"); for (int i = 0; i < uLesson.length(); i++) { HashMap<String, Object> hm; hm = new HashMap<String, Object>(); hm.put(DAY, uDay.getString("day").toString()); hm.put(CLASS, uClass.getString("class").toString()); hm.put(LETTER, uLetter.getString("letter").toString()); hm.put(LESSON, uLesson.getString("lesson").toString()); hm.put(TASK, uTask.getString("task").toString()); myBooks.add(hm); SimpleAdapter adapter = new SimpleAdapter(showHomework.this, myBooks, R.layout.list, new String[] { TASK, CLASS, LETTER, LESSON, DAY}, new int[] { R.id.text1, R.id.text2, R.id.text3, R.id.text4, R.id.text5 }); //выводим в листвью listView.setAdapter(adapter); listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); } } catch (JSONException e) { Log.e("log_tag", "Error parsing data " + e.toString()); } } 

Vooot ... And the Android Studio terminal gives an error:

Error parsing data org.json.JSONException: No value for day.

As I understand my code can not find the corresponding value of the day in json'e ..

Guys, please do not swear, I'm new to this thread .. Help than you can ..

  • one
    Your string is not a valid JSON ohm, so you cannot parse it. - Vladyslav Matviienko
  • one
    I don’t really know Java, but from logic, you have the key in json, for example, day: ', but you try to get the day (without a colon). You or from the array itself, in php, remove the colons from the keys, or get it right - Bookin
  • one
    Yes, and as correctly written above, you do not correctly form json - Bookin

2 answers 2

We form an array, in php, and convert to json string

 $data=[ 'day'=>$row['day'], 'class'=>$row['class'], 'letter'=>$row['letter'], 'lesson'=>$row['lesson'], 'task'=>$row['task'] ]; echo json_decode($data); 

Java example:

 JSONParser parser = new JSONParser(); Object obj = parser.parse(response); JSONObject jsonObj = (JSONObject) obj; System.out.println(jsonObj.get("day")); 

With java not familiar, for this reason - Source

    You get an exception because you are trying to take a value with the wrong key. The server returns the day value to you by the key "day:", and you parse by the key "day". Just add a colon and it will work. (The same in the other fields)