【问题标题】:Json to xml with greek characters带有希腊字符的 Json 到 xml
【发布时间】:2011-08-11 13:52:29
【问题描述】:

我正在使用 curl 获取一个可以位于此处的 json 文件:(复制粘贴太长了):http://www.opap.gr/web/services/rs/betting/availableBetGames/sport/program/4100/0/sport-1.json?localeId=el_GR

之后我使用 json_decode 来获取关联数组。直到这里一切正常。当我使用 var_dump 时,数组中的字符是希腊语。之后我使用以下代码:

    $JsonClass = new ArrayToXML();
    $mydata=$JsonClass->toXml($json);

类 ArrayToXML {

public static function toXML( $data, $rootNodeName = 'ResultSet', &$xml=null ) {

    // turn off compatibility mode as simple xml throws a wobbly if you don't.
   // if ( ini_get('zend.ze1_compatibility_mode') == 1 ) ini_set ( 'zend.ze1_compatibility_mode', 0 );
    if ( is_null( $xml ) ) //$xml = simplexml_load_string( "" );
        $xml = simplexml_load_string("<?xml version='1.0' encoding='UTF-8'?><$rootNodeName />");

    // loop through the data passed in.
    foreach( $data as $key => $value ) {

        $numeric = false;

        // no numeric keys in our xml please!
        if ( is_numeric( $key ) ) {
            $numeric = 1;
            $key = $rootNodeName;
        }

        // delete any char not allowed in XML element names
        `enter code here`$key = preg_replace('/[^a-z0-9\-\_\.\:]/i', '', $key);

        // if there is another array found recrusively call this function
        if ( is_array( $value ) ) {
            $node = ArrayToXML::isAssoc( $value ) || $numeric ? $xml->addChild( $key ) : $xml;

            // recrusive call.
            if ( $numeric ) $key = 'anon';
            ArrayToXML::toXml( $value, $key, $node );
        } else {

            // add single node.
            $value = htmlentities( $value );
            $xml->addChild( $key, $value );
        }
    }

    // pass back as XML
    return $xml->asXML();


}
public static function isAssoc( $array ) {
    return (is_array($array) && 0 !== count(array_diff_key($array, array_keys(array_keys($array)))));
}

}

问题来了。结果中的所有希腊字符都是一些奇怪的字符&amp;Icirc;?&amp;Icirc;?&amp;Icirc;&amp;yen;&amp;Icirc;?&amp;Icirc;?&amp;Icirc;&amp;iexcl;&amp;Icirc;&amp;copy;&amp;Icirc;&amp;pound;&amp;Icirc;?&amp;Icirc;? 例如。我真的不知道我做错了什么。我对编码/解码的东西真的很不好:( .

为了更清楚一点:

下面是关联数组(在我遇到问题的部分)的样子:

{ ["resources"]=> array(4) { ["team-4833"]=> string(24) "ΛΕΥΚΟΡΩΣΙΑ U21" ["t-429"]=> string(72) "ΠΡΟΚΡΙΜΑΤΙΚΑ ΕΥΡΩΠΑΪΚΟΥ ΠΡΩΤΑΘΛΗΜΑΤΟΣ" ["t-429-short"]=> string(6) "ΠΕΠ" ["team-15387"]=> string(16) "ΕΛΛΑΔΑ U21" } ["locale"]=> string(5) "el_GR" } ["relatedNum"]=> NULL }

这是我使用 simplexml 后得到的结果

<resources><team-4833>&Icirc;?&Icirc;?&Icirc;&yen;&Icirc;?&Icirc;?&Icirc;&iexcl;&Icirc;&copy;&Icirc;&pound;&Icirc;?&Icirc;? U21</team-4833><t-429>&Icirc;&nbsp;&Icirc;&iexcl;&Icirc;?&Icirc;?&Icirc;&iexcl;&Icirc;?&Icirc;?&Icirc;?&Icirc;&curren;&Icirc;?&Icirc;?&Icirc;? &Icirc;?&Icirc;&yen;&Icirc;&iexcl;&Icirc;&copy;&Icirc;&nbsp;&Icirc;?&Icirc;&ordf;&Icirc;?&Icirc;?&Icirc;&yen; &Icirc;&nbsp;&Icirc;&iexcl;&Icirc;&copy;&Icirc;&curren;&Icirc;?&Icirc;?&Icirc;?&Icirc;?&Icirc;?&Icirc;?&Icirc;&curren;&Icirc;?&Icirc;&pound;</t-429><t-429-short>&Icirc;&nbsp;&Icirc;?&Icirc;&nbsp;</t-429-short><team-15387>&Icirc;?&Icirc;?&Icirc;?&Icirc;?&Icirc;?&Icirc;? U21</team-15387></resources><locale>el_GR</locale></lexicon><relatedNum></relatedNum></betGames>

提前感谢您的回复。

PS:我也有&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;在我显示结果的页面中,但它没有帮助。


我仍然没有找到解决方案,所以我使用了 Yannis 建议的不同方法。我使用我在此处找到的类 http://www.phpclasses.org/package/1826-PHP-Store-associative-array-data-on-file-in-XML.html 将 XML 保存在一个文件中。

之后,我使用 simplexml_load_file 加载 xml,并使用 xslt 访问所有节点中的数据并将其存储在我的数据库中。那样工作得很好。如果有人仍然想尝试解释为什么它不起作用以我一开始尝试的方式,请随意(仅出于学习目的:p)感谢您的回复:)。

【问题讨论】:

    标签: php json unicode utf-8 simplexml


    【解决方案1】:

    没有必要 - 当前的 json 显然也是以 xml 格式给出的:

    http://www.opap.gr/web/services/rs/betting/availableBetGames/sport/program/4100/0/sport-1.xml?localeId=el_GR

    只需要稍微调整一下 url 参数:)

    【讨论】:

      【解决方案2】:

      这对我使用 php 版本 5.3.6 在 chrome 上有效:

          $json = file_get_contents('http://www.opap.gr/web/services/rs/betting/availableBetGames/sport/program/4100/0/sport-1.json?localeId=el_GR');
          $json = json_decode($json, true);
          $xml = new SimpleXMLElement('<ResultSet/>');
          array_walk_recursive($json, array ($xml, 'addChild'));
          print $xml->asXML();
          exit();
      

      【讨论】:

      • 这只有在数组没有数字元素时才有效。而且 json 的方式总是在关联数组中有数字。所以 xml 将有数字元素。
      【解决方案3】:

      很明显,您的错误是您正在操纵 UTF-8 编码的 Unicode,就好像这些字节是 ISO-8859-1。

      我看不到这是在哪里发生的;可能在您致电htmlentities 时,不管是什么。

      它可能需要使用某种“多字节”破解,可能包括诸如这种模式之类的东西:

      /([^\x00-\x7F])/u
      

      带有明确的/u,因此它适用于逻辑代码点而不是 8 位代码单元(读取:字节)。它可能会这样做以获取一个非 ASCII 代码点,以便可以将其替换为数字实体。如果没有容易忘记的/u,它将适用于字节而不是代码点,这与您的描述显示的情况相符。

      可能是这种情况,或者可能是您必须切换到某些mb_*() 函数而不是普通函数。这是为了解决基本的 PHP 错误,即语言中没有真正的 Unicode 支持,只是到处都有一些创可贴,似乎不时会无缘无故地脱落。

      如果您可以使用一种干净的语言,它不仅支持适当的 Unicode,而且物理字节和抽象字符之间也有清晰的分离,那么这种事情就不会发生。但我敢打赌,这是其他人一定也遇到过的常见问题,所以如果它是库错误而不是代码中某处的(完全可以理解的!)疏忽,我会感到非常惊讶。

      【讨论】:

        【解决方案4】:

        回答来自希腊的问题--------- 单词“?[ΛΕΥΚΟ]”?它有 ASC(他的代码字符)203-197-213-202-207 ()---------- 然而,当你读到他 [prostithete] 206 并将字母翻倍时---------- 但也将代码更改为以下 206-(203-48=155)-206-(197-48=149)-206-(213-48=165)- -206-(213-48=165)-206-(202-48=154)-206-(207-48=159)---------- 因此,如果您找到 206 to >ignore,他们正在检查一个字符的解决方案--------- 他并在下一个字符的 ASC 中添加数字 48 并找到新字符。 >------------ 因为我也处理 [ΟΠΑΠ] 的 [ΑΠΟΚΟΔΙΚΟΠΟΙΗΣΗ] 每一个新知识他们是 >[ΕΥΠΡΟΣΔΕΚΤΟ]------ 在邮件中->? bluegt03@in.gr

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2022-07-06
          • 2022-01-02
          • 1970-01-01
          • 1970-01-01
          • 2012-03-17
          • 1970-01-01
          相关资源
          最近更新 更多