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


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

因此,在一开始,在加载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中的每个字符串都被转换为"message"数组中自己的数组项。我该如何解决这个问题?非常感谢。

您的问题是:

        $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(...)总是比前一行大一个。这不是最后一个索引,而是大小。即CCD_ 2。

无论如何,您应该做的不是使用对象,而是添加一个数组,并同时使用它的所有属性:

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

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