0

好的,所以这个标题可能有点误导,但我不太确定我在描述什么,所以就这样吧。

我有以下 JSON:

{
  "lastModified": 1368517749000,
  "name": "Requiem Paradisum",
  "realm": "Chamber of Aspects",
  "battlegroup": "Misery",
  "level": 25,
  "side": 1,
  "achievementPoints": 1710,
  "emblem": {
    "icon": 126,
    "iconColor": "ffdfa55a",
    "border": 3,
    "borderColor": "ff0f1415",
    "backgroundColor": "ff232323"
  },
  "news": [
    {
      "type": "itemPurchase",
      "character": "Osmoses",
      "timestamp": 1368482100000,
      "itemId": 91781
    },
    {
      "type": "itemLoot",
      "character": "Greenmean",
      "timestamp": 1368477900000,
      "itemId": 87209
    },
    {
      "type": "itemLoot",
      "character": "Greenmean",
      "timestamp": 1368475800000,
      "itemId": 86880
    },
    {
      "type": "itemPurchase",
      "character": "Osmoses",
      "timestamp": 1368475380000,
      "itemId": 91781
    },
    {
      "type": "itemPurchase",
      "character": "Osmoses",
      "timestamp": 1368475380000,
      "itemId": 91779
    },
    {
      "type": "itemPurchase",
      "character": "Osmoses",
      "timestamp": 1368475320000,
      "itemId": 91779
    },
    {
      "type": "playerAchievement",
      "character": "Osmoses",
      "timestamp": 1368470700000,
      "achievement": {
        "id": 6193,
        "title": "Level 90",
        "points": 10,
        "description": "Reach level 90.",
        "rewardItems": [
          {
            "id": 87764,
            "name": "Serpent's Heart Firework",
            "icon": "inv_misc_missilelarge_green",
            "quality": 1,
            "itemLevel": 1,
            "tooltipParams": {

            },
            "stats": [

            ],
            "armor": 0
          }
        ],
        "icon": "achievement_level_90",
        "criteria": [

        ],
        "accountWide": false,
        "factionId": 2
      }
    },

基本上我需要遍历“新闻”中的所有内容并将其输出。我无法弄清楚如何正确解析它: A:没有指定键号和 B:当它到达一个键时,该键然后包含更多键和这些键下的更多数组,我不知所措。(例如“玩家成就”键)我很感激我在这里可能有点新手,很可能在“php for dummies”的第 1 页,但我发誓我已经用谷歌搜索了它!

4

3 回答 3

1

json_decode

不要忘记给出第二个参数,TRUE否则它将返回对象

并尝试这样的事情

 $json         = 'your json'
 $json_array   = json_decode($json,true);

 $news         = $json_array['news'];

foreach($news as $value)
{

     print_r($value);

}

我认为出于您的目的,您应该查看array_walk_recursive

  function printResult($item, $key)
  {
     echo "$key holds $item\n";
  }
  array_walk_recursive($news, 'printResult');
于 2013-05-14T10:55:31.560 回答
1

看看这种方法是否适合。但请确保您的 JSON 数组格式正确。

 $test = the_json_array;    
 $array = json_decode($test,true);
  function recursive($array) {
        foreach($array as $key => $value) {
            if (!is_array($value)) echo $key.":".$value."<br/>";
            else recursive($value);
        }
      }
 recursive($array);
于 2013-05-14T22:56:37.313 回答
0

你需要这样做json_decode('your-jason', True)会将你的 Json 字符串转换为数组。

Json_decode 了解更多

如果你没有TRUEjason_decode函数中指定,那么它将返回 php 对象

希望回答问题。问候

于 2013-05-14T10:52:41.300 回答