繁体   English   中英

PHP:将HTML表解析为JSON

[英]PHP : Parsing HTML table to JSON

我有一个要解析为JSON的HTML表。

<table class="valas" border="0">
        <tr>
            <th>Mata Uang</th><th>Jual</th><th>Beli</th>
        </tr><tr class="odd">
            <td>USD</td><td class="number">11.450,00</td><td class="number">11.300,00</td>
        </tr><tr class="even">
            <td>AUD</td><td class="number">11.094,00</td><td class="number">10.494,00</td>
        </tr><tr class="odd">
            <td>CAD</td><td class="number">11.169,00</td><td class="number">10.669,00</td>
        </tr><tr class="even">
            <td>CHF</td><td class="number">12.719,00</td><td class="number">12.219,00</td>
        </tr><tr class="odd">
            <td>EUR</td><td class="number">15.678,00</td><td class="number">15.028,00</td>
        </tr><tr class="even">
            <td>GBP</td><td class="number">18.525,00</td><td class="number">17.725,00</td>
        </tr><tr class="odd">
            <td>HKD</td><td class="number">1.643,00</td><td class="number">1.293,00</td>
        </tr><tr class="even">
            <td>JPY</td><td class="number">118,87</td><td class="number">113,37</td>
        </tr><tr class="odd">
            <td>SAR</td><td class="number">3.233,00</td><td class="number">2.833,00</td>
        </tr><tr class="even">
            <td>SGD</td><td class="number">9.454,00</td><td class="number">8.854,00</td>
        </tr>
    </table>

我有一些通过谷歌搜索找到的PHP代码:

<?php
include("simple_html_dom.php");
$html = file_get_html('index.html');
$row_count=0;
$json = array();
foreach ($html->find('tr') as $row) {
        $currency = $row->find('td',0)->innertext;
        $sell = $row->find('td',1)->innertext;
        $buy = $row->find('td',2)->innertext;

        $json[$currency][$sell][$buy]=true;
    }
    echo json_encode($json);
   ?>

我得到的是代码,这似乎是错误的:

 {
"":{
    "":{
        "":true
    }
},
"USD":{
    "11.450,00":{
        "11.300,00":true
    }
},
"AUD":{
    "11.094,00":{
        "10.494,00":true
    }
},
"CAD":{
    "11.169,00":{
        "10.669,00":true
    }
},
"CHF":{
    "12.719,00":{
        "12.219,00":true
    }
},
"EUR":{
    "15.678,00":{
        "15.028,00":true
    }
},
"GBP":{
    "18.525,00":{
        "17.725,00":true
    }
},
"HKD":{
    "1.643,00":{
        "1.293,00":true
    }
},
"JPY":{
    "118,87":{
        "113,37":true
    }
},
"SAR":{
    "3.233,00":{
        "2.833,00":true
    }
},
"SGD":{
    "9.454,00":{
        "8.854,00":true
    }
}
}null

但是我需要的是:

 [{
"currency":"USD"{
    "sell":"11.450,00"
    "buy":"11.300,00"
},
"currency":"AUD"{
    "sell":"11.094,00"
    "buy":"10.494,00"
},
"currency":"CAD"{
    "sell":"11.169,00"
        "buy":"10.669,00"
},
"currency":"CHF"{
    "sell":"12.719,00"
    "buy":"12.219,00"
},
"currency":"EUR"{
    "sell":"15.678,00"
    "buy":"15.028,00"
},
"currency":"GBP"{
    "sell":"18.525,00"
    "buy":"17.725,00"
},
"currency":"HKD"{
    "sell":"1.643,00"
    "buy":"1.293,00"
},
"currency":"JPY"{
    "sell":"118,87"
    "buy":"113,37"
},
"currency":"SAR"{
    "sell":"3.233,00"
    "buy":"2.833,00"
},
"currency""SGD"{
    "sell":"9.454,00"
    "boy":"8.854,00"
}
}]

我认为问题出在$ json变量中,但很难实现。

尽管您需要的不是有效的JSON,但您可以通过以下方式实现正确的结构:

$json[] = [ 'currency' => $currency, 'sell' => $sell, 'buy' => $buy ];

我相信这就是你想要的.....

更改此行:

$json[$currency][$sell][$buy]=true;

至:

$json[$row_count]['currency'] = $currency;
$json[$row_count]['sell'] = $sell;
$json[$row_count]['buy'] = $buy;

$row_count++;

如果您从此处获取代码: https : //github.com/tremblay/HTML-Table-to-JSON ,则可以这样获得所需的答案:

htmlToJSON('index.html',FALSE,null,null,array(0 =>'Currency',1 =>'Buy',2 =>'Sell'),null,null,TRUE);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM