解析所有 JSON 参数和子集


parsing all json parametr and subset

  {"result":"success","totalresults":2,"products":{"product":[{"pid":"1","gid":"1","type":"hostingaccount","name":"test1","description":"","module":"cpanel","paytype":"onetime","pricing":{"USD":{"prefix":"$","suffix":" USD","msetupfee":"0.00","qsetupfee":"0.00","ssetupfee":"0.00","asetupfee":"0.00","bsetupfee":"0.00","tsetupfee":"0.00","monthly":"0.00","quarterly":"0.00","semiannually":"0.00","annually":"-1.00","biennially":"-1.00","triennially":"-1.00"}},"customfields":{"customfield":[]},"configoptions":{"configoption":[]}},{"pid":"2","gid":"1","type":"other","name":"javad host","description":"","module":"","paytype":"recurring","pricing":{"USD":{"prefix":"$","suffix":" USD","msetupfee":"12.00","qsetupfee":"25.00","ssetupfee":"0.00","asetupfee":"0.00","bsetupfee":"0.00","tsetupfee":"0.00","monthly":"24.00","quarterly":"26.00","semiannually":"-1.00","annually":"-1.00","biennially":"-1.00","triennially":"-1.00"}},"customfields":{"customfield":[]},"configoptions":{"configoption":[]}}]}}

这是我的 JSON - 我使用此代码:

<?php
$url = 'http://safemode.acloud.ir/rasanegar/currencly.php';
$content = file_get_contents($url);
$json = json_decode($content, true);
foreach($json['products']['product']as $item) {
echo '<tr>';
echo '<td>'.$item['pid'].'</td>';
echo '<td>'.$item['type'].'</td>';
echo '</tr>';
}?>

但是我可以解析产品 json,例如"pid"、"gid",我不能解析定价子集,例如"前缀"、"每月">

为什么我们需要为您调查您的数据结构?

下次请自己动手:

{"result":"success",
"totalresults":2,
"products":{
    "product":[
        {
            "pid":"1",
            "gid":"1",
            "type":"hostingaccount",
            "name":"test1",
            "description":"",
            "module":"cpanel",
            "paytype":"onetime",
            "pricing":{
                "USD":{
                    "prefix":"$",
                    "suffix":" USD",
                    "msetupfee":"0.00",
                    "qsetupfee":"0.00",
                    "ssetupfee":"0.00",
                    "asetupfee":"0.00",
                    "bsetupfee":"0.00",
                    "tsetupfee":"0.00",
                    "monthly":"0.00",
                    "quarterly":"0.00",
                    "semiannually":"0.00",
                    "annually":"-1.00",
                    "biennially":"-1.00",
                    "triennially":"-1.00"
                }
            },
            "customfields":{
                "customfield":[]
            },
            "configoptions":{
                "configoption":[]
            }
        },
....

正如@MarkBaker在评论中写道:

<?php
$url = 'http://safemode.acloud.ir/rasanegar/currencly.php';
$content = file_get_contents($url);
$json = json_decode($content, true);
foreach($json['products']['product']as $item) {
echo '<tr>';
echo '<td>'.$item['pid'].'</td>';
echo '<td>'.$item['type'].'</td>';
echo '<td>'.$item['pricing']['USD']['prefix'].'</td>';
echo '<td>'.$item['pricing']['USD']['monthly'].'</td>';
echo '</tr>';
}?>