PHP 从 JSON 转换为 stdClass、进行更改然后转换回 JSON 时遇到问题

发布于 2024-12-26 01:33:45 字数 4668 浏览 3 评论 0原文

因此,一开始,在加载 send_sms.php 之前,我将此 Json 存储在数据库中:

{
"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"
            }]
        }
    }]
}
}

Json 完全有效,没有错误。它存储两个聊天记录,其中包含消息。

现在,这是更改 Json 的 PHP 代码:

    $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;
        }
    }

我的目的是在聊天的“message”数组中添加另一个 stdClass 对象。所以我基本上就是这么做的,希望它能奏效。

现在,它可以工作了,但是这是我们对它进行 json_encode 后的新 Json:

{
"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"
            }]
        }
    }]
}
}

您会注意到它确实被添加到“Ethan Wilberforce”聊天中,但是 stdClass 中的每个字符串都被转换为“消息”数组。我该如何解决这个问题?非常感谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

耶耶耶 2025-01-02 01:33:45

你的问题是这样的:

        $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);

它基本上相当于:

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

你不断追加新的数组/对象,因为你使用 sizeof(...) 它总是比前一行大。它不是最后一个索引,而是大小。这是$lastindex+1

无论如何,你应该做的不是使用对象,而只是附加一个数组,并立即添加它的所有属性:

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

当你将该关联数组编码回 JSON 时,它也将成为一个普通的 {...} JSON 对象组。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文