1

因此,在一开始,在加载 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;
        }
    }

我打算这样做是在聊天的“消息”数组中添加另一个 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”数组中自己的数组项。我该如何解决这个问题?非常感谢。

4

1 回答 1

3

你的问题是这样的:

        $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 对象组。

于 2012-01-09T00:05:21.340 回答