使用来自Mongo的PHP解码JSON [英] Decoding JSON using PHP from Mongo

查看:97
本文介绍了使用来自Mongo的PHP解码JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看过以下线程: PHP解码嵌套的JSON ,但没有设法用它来解决我的问题.

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

我目前正在从Mongo抓取JSON对象,并且在从嵌套对象抓取信息时遇到问题.

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),
}

我的目的是打印出每个项目的信息,我的PHP代码如下

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.

谢谢.

推荐答案

MongoDB不会将数据存储为JSON.

MongoDB does not store the data as JSON.

您粘贴的文档似乎是从shell复制并粘贴的.这也不是JSON格式. 这种格式称为扩展JSON-以及MongoDB Shell在JavaScript中表示数据的方式. MongoDB网站上的大多数示例都使用这种格式,因为它既简单又容易.因此,MongoDB官方文档不是使用数十种不同的输出格式(针对每种语言驱动程序),而是使用Shell来演示功能和输出.

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.

实际的下标格式称为二进制JSON(BSON).您将永远不会看到这种格式,也永远不会与之交互.

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

当您在PHP中与MongoDB进行交互时,您只需要保存一个PHP数组即可.从MongoDB返回的数据也是一个PHP数组.底层磁盘格式不相关.

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.

您不必调用json_encode()或json_decode().

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

$collection->find($query)方法返回一个名为MongoCursor的对象.您应该对该对象进行迭代以获得结果,该结果将是一个PHP数组.

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

此代码示例一次将var_dump()一个结果.此result称为"MongoDB文档",类似于"MySQL行".就像使用MySQL一样,您不必知道底层协议是什么,或者底层磁盘格式是什么-对您没有影响.

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.

我强烈建议您阅读MongoDB PHP驱动程序教程: http://us2 .php.net/manual/en/mongo.tutorial.php

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 :)

这篇关于使用来自Mongo的PHP解码JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆