dongtaigan1594 2014-04-10 20:34
浏览 58
已采纳

使用Mongo中的PHP解码JSON

I've already looked at this thread: PHP decode nested JSON and haven't managed to use it to solve my problem.

I am currently grabbing a JSON object from Mongo, and I'm having issues grabbing information from nested objects.

{
"adminLevel" : 200,
    "chat" : true,
    "clans" : [
            BinData(0,"wcXHR577OVBXfy9JwEf5gQAAAAAAAAAAAAAAAAAAAAAAAAAAAA")
    ],
    "experience" : NumberLong(70003),
    "kitNew" : {
            "converted" : true,
            "items" : {
                    "ak47" : {
                            "killCount" : 5,
                            "selected" : false,
                            "unlocked" : 1
                    },
                    "hub-knife" : {
                            "selected" : false
                    },
                    "assault" : {
                            "selected" : false,
                            "unlocked" : 1
                    },
                    "pistol" : {
                            "deathWhileSelectedCount" : 3,
                            "killedBySelectedCount" : 1,
                            "killWhileSelectedCount" : 1,
                            "selected" : false,
                            "unlocked" : 1
                    },

                    "m1014" : {
                            "deathWhileSelectedCount" : 3,
                            "killedBySelectedCount" : 1,
                            "killCount" : 17,
                            "killWhileSelectedCount" : 1,
                            "killedByCount" : 1,
                            "selected" : false,
                            "unlocked" : 1
                    },
            },
    },
    "points" : NumberLong(87167),
}

My aim here is to print out information about each of the items, my PHP code is as follows

// execute query
// retrieve all documents
$query = array("lastKnownUsername"  => "Strubo");
$cursor = $collection->find($query);
$array = json_decode($cursor, true);
$items = $array->kitNew->items;

foreach($items as $item){
    echo "<tr>";
        echo "<td>" . $item . "</td>";
        echo "<td>" . $item->killCount . "</td>";
    echo "<td>" . $item->killedByCount . "</td>";
        echo "<td>" . $item->selected . "</td>";
    echo "<td>" . $item->unlocked . "</td>";
    echo "</tr>";
}

// disconnect from server
$conn->close();
} catch (MongoConnectionException $e) {
        die('Error connecting to MongoDB server');
} catch (MongoException $e) {
    die('Error: ' . $e->getMessage());
}

I know that there is no problems connecting to the database, or any environment issues, as other displays of grabbed data work. It's just nesting which is the issue here.

Thanks.

  • 写回答

1条回答 默认 最新

  • doumi0737 2014-04-10 23:12
    关注

    MongoDB does not store the data as JSON.

    The document you pasted seems to be copy&paste from the shell. This is not JSON format either. This format is called Extended JSON - and is how the MongoDB shell represents the data in javascript. Most examples on the MongoDB website also use this format as it is simple and easy. So rather then have dozens of different output format (for each language driver), the MongoDB official documentations use the shell for demonstrating functionality and output.

    The actual underlaying format is called Binary JSON (BSON). You will never see this format and you will never interact with it.

    When you interact with MongoDB in PHP all you have to know is that you save a PHP array. The data returned from MongoDB is also a PHP array. The underlaying disk format is not relevant.

    You never have to call json_encode() or json_decode().

    The $collection->find($query) method returns a object called MongoCursor. You should iterate over this object to get the results, which will be a PHP array.

    foreach($collection->find($query) as $result) {
        var_dump($result);
    }
    

    This code example will var_dump() one result at a time. This result is called a "MongoDB Document" and is similar to "MySQL row". Just like with MySQL, you don't have to know what the underlaying protocol is, or what the underlaying disk format is - that has no affect on you.

    I strongly suggest you read the MongoDB PHP Driver tutorial: http://us2.php.net/manual/en/mongo.tutorial.php

    This should explain the concept a littlebit better, along with how the driver works :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办