We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 24865
    • 289 Posts
    Hi guys,

    I’m busy developing a program and I came accross a little problem. I’m outputting 3 countries, The Netherlands, Germany and Belgium. Now, in their Dutch counterparts it’s Nederland, Duitsland and België. Notice the "ë" on België? That ë comes through as "null" in JSON encode. Seeing that I am running PHP 5.3, MODx defaults to json_encode as it primary json encoding function. I fixed the problem pre-looping every value as utf8_encode.

    Now the real... point is, perhaps it could be a good idea to recursively loop over all the values and transform these to utf8 encoded values. Naturally, this would be optional but I reckon, through JSON, this would be desirable.

    Lemme know what you guys think and IF I am missing something.
      @MarkGHErnst

      Developer at Adwise Internetmarketing, the Netherlands.
      • 22303 MODX Staff
      • 10,725 Posts
      JSON does not support non-UTF8 data; I imagine you are using characters from some other encoding or your database is not using a utf8 collation? I can’t reproduce the problem here.
        • 24865
        • 289 Posts
        Ah indeed, the encoding is set on latin1_swedish, I’m gonna attempt the same while I’ve set it on utf8_general.

        /edit
        Alright, after setting it on utf8_general, it has reverted to "null".

        {"success":true,"message":"","total":2,"data":[{"id":1,"short":"nl","long":"Nederland"},{"id":2,"short":"be","long":null}],"object":[]}


        Notice the "long":null portion of the JSON? Well, that’s the problem. I’ve already tried;

        • Table coalition change to utf8_general_ci or _unicode_ci
        • Database coalition change to utf8_general_ci or _unicode_ci
        • PHP Content-type json & utf8 charset above output

        My installation is local, but that shouldn’t matter.

        /edit
        The following test scenario works fine;
        echo '<pre>';
                    
                    $countries = $this->modx->getCollection('testCountries');
                    header('Content-type: text/html; charset=ascii');
                    foreach($countries as $country) {
                        print_r($country->toArray());
                    }
                    exit();

        But I do not want it to be encoded as ascii, but utf-8, since that’s the mainstream encoding.

        /another edit
        Fixed the MySQL charset from latin1 to utf8;
        MySQL Karakterset:  UTF-8 Unicode (utf8) 

        Translated: MySQL Charset: UTF-8 Unicode
          @MarkGHErnst

          Developer at Adwise Internetmarketing, the Netherlands.