PHP Troubles with converting from JSON to stdClass, making changes, and then converting back to JSON

StackOverflow https://stackoverflow.com/questions/8782522

  •  15-04-2021
  •  | 
  •  

문제

So, in the very beginning, before the send_sms.php is loaded, I have this Json stored in a database:

{
"chats": {
    "chat": [{
        "id": "1",
        "name": "Ethan Wilberforce",
        "messages": {
            "message": [{
                "id": "1",
                "name": "Ethan Wilberforce",
                "text": "Hello how are you doing",
                "time": "4:41"
            }, {
                "id": "2",
                "name": "Qasim Iqbal",
                "text": "Not bad. How about you?",
                "time": "4:42"
            }, {
                "id": "3",
                "name": "Ethan Wilberforce",
                "text": "I'm not too bad myself.",
                "time": "4:43"
            }]
        }
    }, {
        "id": "2",
        "name": "Geoff Vahaaho",
        "messages": {
            "message": [{
                "id": "1",
                "name": "Geoff Vahaaho",
                "text": "Hello how are you doing",
                "time": "4:41"
            }, {
                "id": "2",
                "name": "Qasim Iqbal",
                "text": "Not bad. How about you?",
                "time": "4:42"
            }, {
                "id": "3",
                "name": "Geoff Vahaaho",
                "text": "I'm not too bad myself.",
                "time": "4:43"
            }, {
                "id": "4",
                "name": "Qasim Iqbal",
                "text": "Nice.",
                "time": "4:43"
            }]
        }
    }]
}
}

The Json is completely valid, no errors. It is storing two chats, with messages in them.

Now, here is the PHP code that alters the Json:

    $data = $user->data;
    $parsed_data = json_decode($data);

        ...

    for($i = 0, $size = sizeof($parsed_data->chats->chat); $i < $size; ++$i) {
        if($parsed_data->chats->chat[$i]->name == $to) {
            $found = true;
            $parsed_data->chats->chat[$i]->messages->message[sizeof($parsed_data->chats->chat[$i]->messages->message)] = new stdClass;
            $parsed_data->chats->chat[$i]->messages->message[sizeof($parsed_data->chats->chat[$i]->messages->message)]->id = sizeof($parsed_data->chats->chat[$i]->messages->message);
            $parsed_data->chats->chat[$i]->messages->message[sizeof($parsed_data->chats->chat[$i]->messages->message)]->name = $user->name;
            $parsed_data->chats->chat[$i]->messages->message[sizeof($parsed_data->chats->chat[$i]->messages->message)]->text = $message;
            $parsed_data->chats->chat[$i]->messages->message[sizeof($parsed_data->chats->chat[$i]->messages->message)]->time = $time;
            echo "done. ";
            break;
        }
    }

What I intend this to do is, to add another stdClass object in the "message" array of the chat. So I basically do just that, hoping it will work.

Now, it works, kind of, but here is the new Json after we json_encode it:

{
"chats": {
    "chat": [{
        "id": "1",
        "name": "Ethan Wilberforce",
        "messages": {
            "message": [{
                "id": "1",
                "name": "Ethan Wilberforce",
                "text": "Hello how are you doing",
                "time": "4:41"
            }, {
                "id": "2",
                "name": "Qasim Iqbal",
                "text": "Not bad. How about you?",
                "time": "4:42"
            }, {
                "id": "3",
                "name": "Ethan Wilberforce",
                "text": "I'm not too bad myself.",
                "time": "4:43"
            }, {}, {
                "id": 4
            }, {
                "name": "Qasim Iqbal"
            }, {
                "text": "Hello i am testing"
            }, {
                "time": 1326066200
            }]
        }
    }, {
        "id": "2",
        "name": "Geoff Vahaaho",
        "messages": {
            "message": [{
                "id": "1",
                "name": "Geoff Vahaaho",
                "text": "Hello how are you doing",
                "time": "4:41"
            }, {
                "id": "2",
                "name": "Qasim Iqbal",
                "text": "Not bad. How about you?",
                "time": "4:42"
            }, {
                "id": "3",
                "name": "Geoff Vahaaho",
                "text": "I'm not too bad myself.",
                "time": "4:43"
            }, {
                "id": "4",
                "name": "Qasim Iqbal",
                "text": "Nice.",
                "time": "4:43"
            }]
        }
    }]
}
}

You will notice it was indeed added in the "Ethan Wilberforce" chat, but each string in the stdClass was converted to its own array item in the "message" array. How could I fix this problem? Many thanks.

도움이 되었습니까?

해결책

Your problem is this:

        $parsed_data->chats->chat[$i]->messages->message[sizeof($parsed_data->chats->chat[$i]->messages->message)] = new stdClass;
        $parsed_data->chats->chat[$i]->messages->message[sizeof($parsed_data->chats->chat[$i]->messages->message)]->id = sizeof($parsed_data->chats->chat[$i]->messages->message);

It basically amounts to:

$array[$last] = new stdClass();
$array[$last+1] = "id";
$array[$last+2] = "name";

You keep appending new arrays/objects, because you use sizeof(...) which always becomes one larger than the previous line. It's not the last index, but the size. Which is $lastindex+1.

What you should be doing anyway, is not using an object, but just appending an array, and with all its attributes at once:

 $parsed_data->chats->chat[$i]->messages->message[] = array(
      "id" => ...,
      "name" => ...,
      "time" => ...,
 );

When you encode that associative array back into JSON it will also become a normal {...} JSON object group.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top