简体   繁体   English

如何为php中的UTF-8字符从mysql数据库返回的json数据创建服装布局

[英]how to create a costume layout for json data returned from mysql database for UTF-8 characters in php

I am trying to create a json representation of mysql data in a restful webservice using php. 我正在尝试使用php在一个宁静的Web服务中创建mysql数据的json表示形式。 my returned json should look something like this: 我返回的json应该看起来像这样:

"worktypes" :
[
    {
        "id" : "1",
        "name" : "نوع 1",
        "isactive" : "true"
    },
    {
        "id" : "2",
        "name" : "نوع 2",
        "isactive" : "false"
    }
]

one of my options was to iterate through my result records and echo each line by hand. 我的选择之一是遍历我的结果记录并手动echo每一行。 It was working until I reached utf8 characters shown above (like 'نوع 1') and instead of ending up with something like \Ù\†\Ù\ˆ\Ø\¹ I got unreadable symbols like ÃÂÃÂù çÃÂà. 一直工作到我到达上面显示的utf8字符(如'نوع1'),而不是最后得到\Ù\†\Ù\ˆ\Ø\¹我得到了诸如ÃÂÃÂù çÃÂÃÂ不可读符号。

If I could use json_encode somehow to create this layout, it would automatically solve this problem;But I don't know how to create such a structure in php that its json_encoded string represents my desired layout. 如果我可以通过某种方式使用json_encode创建此布局,它将自动解决此问题;但是我不知道如何在php中创建这样的结构,使其json_encoded字符串代表我想要的布局。

My other option is to somehow encode these characters myself. 我的另一个选择是自己对这些字符进行编码。 But I don't know how to do that either. 但是我也不知道该怎么做。

Could anyone help me with my problem? 有人可以帮我解决我的问题吗?

UPDATE: I use a simple echo in my webservice when I get unusual characters like : 更新:当我得到如下异常字符时,我在Web服务中使用了一个简单的echo

$result = array("1" => "نوع 1")
echo $result;

//results in 'ÙÙع 1' when called using webservice

The equivalent PHP array to what you have given is: 与您提供的等效的PHP数组是:

$x = array(
    "worktypes" => array( 
        array(
            "id" => "1",
            "name" => "نوع 1",
            "isactive" => "true"
        ),
        array(
            "id" => "2",
            "name" => "نوع 2",
            "isactive" => "false"
        )
    )
);

You can then use json_encode . 然后,您可以使用json_encode

Note that you may still see escaped characters, but the browser should be able to handle unescaping them when it parses the JSON. 请注意,您仍然可能会看到转义字符,但是浏览器在解析JSON时应该能够处理转义字符。

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

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