I
I
Ilya Bobkov2015-11-09 20:08:00
PHP
Ilya Bobkov, 2015-11-09 20:08:00

Why not parse the JSON response?

Hey! I can not parse the json response for the umpteenth time.
Answer:
{"id":"1","fio":"hhhhhh","pn":"1","vt":"1","sr":"1","cht":"1 ","pt":"2","sb":"1","vs":"1","user_id":"154","group_id":"0","data_reg":"2015-11 -18"}{"id":"2","fio":"sdfsdfsdf","pn":"2","vt":"3","sr":"1","cht":" 2","pt":"3","sb":"1","vs":"2","user_id":"154","group_id":"0","data_reg":"








if ( $_GET["func"] == "GetPersonal" )
{
 $result = $mysqli->query( "SELECT * FROM personal WHERE user_id='".$_SESSION['user_id']."'" );
 if (!$result) error($mysqli->error);
 if ( $result->num_rows > 0 )
 {  
   while($row = $result->fetch_assoc()) 
   {
    $myArray[] = $row;    
   }
 echo json_encode($row);
 }
 else
 {
  echo json_decode( array( "answer" => "empty" ) );
 }

 $result->free();
 $result->close();
 $mysqli->close();
 exit();
}

Answer the question

In order to leave comments, you need to log in

5 answer(s)
D
DevMan, 2015-11-09
@heksen

you do not need to output each row, but collect them in an array and call json_encode once at the end of the script.

if ( $result->num_rows > 0 )
 {  
   while($row = $result->fetch_assoc()) 
   {
    $myArray[] = $row;
   }
   echo json_encode($myArray);
 }

D
Dmitry Skogorev, 2015-11-09
@EnterSandman

Enclose in square brackets

S
Stalker_RED, 2015-11-09
@Stalker_RED

jsfiddle.net/zdbhvdr5
You {...}{...}should have a [{...},{...}]
Replace

while($row = $result->fetch_assoc()) {
    $myArray[] = $row;
    echo json_encode($row);
}

on the
while($row = $result->fetch_assoc()) {
    $myArray[] = $row;
}
echo json_encode($myArray);

V
Vitaly Inchin ☢, 2015-11-09
@In4in

The thing is that in your JSON (or rather, this is not a correct json) several objects are displayed in a row, you can’t do this. They are not part of a large whole and, accordingly, cannot be parsed together..enes..senes? converted to a JavaScript entity. You need to change the answer to:

[
    {"id":"1","fio":"hhhhhh","pn":"1","vt":"1","sr":"1","cht":"1","pt":"2","sb":"1","vs":"1","user_id":"154","group_id":"0","data_reg":"2015-11-18"},
    {"id":"2","fio":"sdfsdfsdf","pn":"2","vt":"3","sr":"1","cht":"2","pt":"3","sb":"1","vs":"2","user_id":"154","group_id":"0","data_reg":"2015-11-18"}
]

°•Something like that•°
if ( $_GET["func"] == "GetPersonal" ){
  $result = $mysqli->query( "SELECT * FROM personal WHERE user_id='".$_SESSION['user_id']."'" );
  if (!$result) error($mysqli->error);

  if ( $result->num_rows > 0 ) {  
    while($row = $result->fetch_assoc()) {
      $myArray[] = $row;    
    }
    echo json_encode($myArray);
  } else {
    echo json_decode('{"answer" : "empty"}');
  }

 $result->free();
 $result->close();
 $mysqli->close();
 exit();
}

var obj = JSON.parse(data);

obj.forEach(function(current){
   console.log(currenr.fio);
});

I
Ivanq, 2015-11-09
@Ivanq

A character with the code 65279 is inserted at the beginning of the line!
UPD: https://yadi.sk/i/nhntEiBTkLiFN

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question