javascript - Parsing JSON object -


how can parse 2 json objects? e.g

ajax return just one object appear's correctly.

object {32 : "joseph"}  

but, when return more 2 objects, i've got this:

responsetext:  "{"users":{"32":"jospeh"}}{"users":{"48":"jospeh k."}}" 

i tried parse json.parse returns error: "uncaught syntaxerror: unexpected token {"

so, how can parse return this:?

 object {32 : "joseph"}   object {48 : "joseph k"}  

instead of "responsetext"

considerations:

  1. if returns 1 object, appears correctly in console(example);
  2. if returns more 2 objects, appears responsetext;
  3. ajax datatype: json

i'll grateful if can this. =d

php:

public function get_error_message() {      $message = "";     foreach ($this->errors $value) {          if ($value instanceof userserror) {             $message.= json_encode(array('errors' => array($value->getcode() => $value->getmessage())));          }     }      return $message; } 

the problem in php code. can't concatenate json , think valid.

instead of

$message = ""; foreach ($this->errors $value) {      if ($value instanceof userserror) {         $message.= json_encode(array('errors' => array($value->getcode() => $value->getmessage())));     } } 

you should generate proper php object, , encode json once.
example, in case, can array:

$errorsarray = array(); foreach ($this->errors $value) {     if ($value instanceof userserror) {         $errorsarray[] = array($value->getcode() => $value->getmessage());     } }  echo json_encode(array('errors' => $errorsarray)); 

then, result following no matter how many objects returns - none,

{     "errors": [] } 

only 1 or

{     "errors": [         {"32": "joseph"}     ] } 

many

{     "errors": [         {"32": "joseph"},         {"48": "joseph k."}     ] } 

in javascript able do:

$.ajax({}).done(function(data) {     console.log(data.errors.length); }); 

you can generate json response more convenient work in javascript. it's requirements , fantasy.

for example, single associated array:

php:

$errorsarray = array(); foreach ($this->errors $value) {     if ($value instanceof userserror) {         $errorsarray[$value->getcode()] = $value->getmessage();     } }  echo json_encode(array('errors' => $errorsarray)); 

json:

{     "errors": {         "32": "joseph",         "48": "joseph k."     } } 

Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -