PHP从JSON转换为stdClass,进行更改,然后转换回JSON,从而引发麻烦

时间:2012-01-08 23:52:02

标签: php arrays json stdclass

所以,在开始之前,在加载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”数组中自己的数组项。我怎么能解决这个问题?非常感谢。

1 个答案:

答案 0 :(得分: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对象组。