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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。