解析JSON对象

How I can parse 2 JSON objects? e.g

AJAX return just one Object appear's correctly.

Object {32 : "Joseph"} 

But, when return more than 2 objects, I've got this:

ResponseText:  "{"users":{"32":"Jospeh"}}{"users":{"48":"Jospeh K."}}"

I already trieD to parse with JSON.parse but returns an error: "Uncaught SyntaxError: Unexpected token {"

So, How I can parse to return something like this:?

 Object {32 : "Joseph"} 
 Object {48 : "Joseph K"} 

Instead of "responseText"

Considerations:

  1. If returns just ONE object, appears correctly in console(example);
  2. If returns more than TWO objects, appears responseText;
  3. AJAX dataType: JSON

I'll be very grateful if someone can help with this. =D

PHP:

public function get_error_message()
{

    $message = "";
    foreach ($this->errors as $value) {

        if ($value instanceof UsersError) {
            $message.= json_encode(array('errors' => array($value->getCode() => $value->getMessage())));

        }
    }

    return $message;
}

That's not a valid JSON object. It is 2 JSON object in string representation concatenated together, which as far as I know is not valid. If you can't change the back-end, and you have to process this and can guarantee it is always 2 JSON object concatenated together, then you'll have to use string parsing to split them before sending them into JSON.parse(...) individually to get 2 JSON objects back.

this is not really standard way and your json is not valid:

but try this

str_data =  "{'users':{'32':'Jospeh'}}{'users':{'48':'Jospeh K.'}}";
str_data = str_data .replace("}{", "}}{{");
items = str_data .split("}{");
object_items = [];
for(i =0; i < items.length; i++){ object_items[i] = JSON.parse(items[i]); }

The problem is in your PHP code. You can't simply concatenate JSON and think that it will be valid.

Instead of

$message = "";
foreach ($this->errors as $value) {

    if ($value instanceof UsersError) {
        $message.= json_encode(array('errors' => array($value->getCode() => $value->getMessage())));
    }
}

you should generate a proper PHP object, and then encode it to JSON once.
For example, in your case, it can be an array:

$errorsArray = array();
foreach ($this->errors as $value) {
    if ($value instanceof UsersError) {
        $errorsArray[] = array($value->getCode() => $value->getMessage());
    }
}

echo json_encode(array('errors' => $errorsArray));

Then, a result will be the following no matter how many objects it returns - none,

{
    "errors": []
}

only one or

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

many

{
    "errors": [
        {"32": "Joseph"},
        {"48": "Joseph K."}
    ]
}

In JavaScript you will be able to do:

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

You can generate another JSON response which will be more convenient to work with in JavaScript. It's up to your requirements and fantasy.

For example, I would do a single associated array:

PHP:

$errorsArray = array();
foreach ($this->errors as $value) {
    if ($value instanceof UsersError) {
        $errorsArray[$value->getCode()] = $value->getMessage();
    }
}

echo json_encode(array('errors' => $errorsArray));

JSON:

{
    "errors": {
        "32": "Joseph",
        "48": "Joseph K."
    }
}

Your JSON is invalid, try push users into an array.

var obj =  {
    "users": [
        {
            "name": "Jospeh",
            "age": 32
        },
        {
            "name": "Joseph K",
            "age": 48
        }
    ]
 }

And for new users just push...

obj.users.push({ "name": "Neil A.", "age": 72 });