首页 > 解决方案 > 在 PHP 中解析 JSON 键和值

问题描述

这些是 JSON 中的值

"_id": "5db81ae803f7410018f7c081",
            "hotness": 72793.81406699134,
            "activityHotness": 0.10295588022415446,
            "primaryCategory": "Exchanges",
            "words": 443,
            "similarArticles": [],
            "coins": [
                {
                    "_id": "59cb59e81c073f09e76f614b",
                    "name": "Bitcoin",
                    "slug": "bitcoin",
                    "tradingSymbol": "btc"
                },
                {
                    "_id": "59d21e9b83a0523906a45dc5",
                    "name": "EOS",
                    "slug": "eos",
                    "tradingSymbol": "eos"
                },
                {
                    "_id": "59d21e9b83a0523906a45dbe",
                    "name": "Tether",
                    "slug": "tether",
                    "tradingSymbol": "usdt"
                }
            ],
            "description": "The world’s 5th largest crypto exchange OKEx is planning to launch Tether futures trading, offering a linear futures contract with leverage of up to 100x.\nThe world’s 5th largest crypto exchange OKEx is planning to launch Tether ( USDT ) futures trading, offering a linear futures contract with…",
            "publishedAt": "2019-10-29T10:16:00.000Z",
            "title": "OKEx to Launch USDT Futures Trading With Up to 100x Leverage",
            "url": "https://cryptocontrol.io/r/api/article/5db81ae803f7410018f7c081?ref=5d9f090e03f7410018f41938",
            "source": {
                "_id": "59d70be3ef8bf95cc2aa2b4f",
                "name": "CoinTelegraph",
                "url": "https://cointelegraph.com/",
                "favicon": "https://assets.cryptocontrol.io/favicons/59d70be3ef8bf95cc2aa2b4f.png"
            },
            "sourceDomain": "cointelegraph.com",
            "originalImageUrl": "https://images.cointelegraph.com/images/740_IGh0dHBzOi8vczMuY29pbnRlbGVncmFwaC5jb20vc3RvcmFnZS91cGxvYWRzL3ZpZXcvMWY5YTllNWViMGI1NTNhMWJkNWVlYjBhZWNkOTAxYzkuanBn.jpg"
        },

我想将 Id 中的值截断为硬币并显示从描述开始的值我试图从区块链数组中的 JSON 文件中获取键和值。但我想截断其中的一些。不知道该怎么做。

我试过使用 foreach 循环

<?php 
$getJsonData = file_get_contents("sample.json");
$jsonArray = json_decode($getJsonData);
$mainName = "blockchain";

$i = 1;
foreach($jsonArray->$mainName as $row){
    echo "<br>----------record $i start <br><br>";

    foreach($row as $key => $val){
        if(is_object($val)){
            foreach($val as $ky => $v1){
                echo $key.' => '.$ky.': '.$v1;
                echo '<br>';
            }
        }else{
            echo $key.': '.$val;
            echo '<br>';
        } 
    }
    echo "<br>----- record $i end <br><br>";
    $i++;
}
?>

标签: phpjson

解决方案


由于您json_decode()在没有可选第二个参数的情况下调用,因此 JSON 对象被解码为 PHP 对象,而不是关联数组。所以你不能foreach()用来循环对象属性。

使用json_decode($getJsonData, true),然后所有对象将成为关联数组。

然后您可以使用array_keys()获取所有键,并删除 和 之间的所有_idcoins

$keys = array_keys($jsonArray[$mainName][0]);
$id_index = array_search('_id', $keys);
$coins_index = array_search('coins', $keys);
array_splice($keys, $id_index, $coins_index - $id_index + 1); // remove keys from _id to coins
foreach ($jsonArray[$mainName] as $row) {
    foreach ($keys as $key) {
        $val = $row[$key];
        if (is_array($val)) {
            foreach ($val as $k => $v) {
                echo "$key => $k: $v<br>";
            }
        } else {
            echo "$key: $val<br>";
        }
    }
}

推荐阅读