代码之家  ›  专栏  ›  技术社区  ›  noyruto88

C++中汉字字符的处理

  •  2
  • noyruto88  · 技术社区  · 8 年前

    我有一个用C++编写的Windows桌面应用程序(命名为:时间戳),使用.NET调用CLR.< /P>

    我还用DLL C++编写了名为AMSCPPRET的Project项目,并使用CPPRESTSDK从服务器获取JSON数据,并将数据传递给我的时间戳应用程序。

    下面是场景: 这是从我的服务器返回的JSON数据,它是一个员工姓名列表,其中大部分是用日文汉字书写的姓名。

    。[
    {
    “工作人员”:。{
    “ID”:121,
    “name”:“___”,
    “部门”:。[
    {
    “U id”:3,
    “name”:“_¨”
    }
    ]
    }
    },请
    {
    “工作人员”:。{
    “ID”:12,
    “name”:“_”,
    “部门”:。[
    {
    “U id”:4,
    “name”:“_¨”
    }
    ]
    }
    },请
    {
    “工作人员”:。{
    “ID”:151,
    “name”:“·§”,
    “部门”:。[
    {
    “U id”:3,
    “name”:“¨_¨”
    }
    ]
    }
    }
    ]
    

    这是我的dll项目(amscpsrest)中的代码。这是获取数据并传递到我的clr项目的方法:

    STD:代码> STD::MAP& LT;int,STD::String & Gt;Stabmap; STD:GSTD::String & Gt;{Stand:; 返回工作人员表; } void显示\ujson(json::value const&jvalue,utility::string const&prefix) { 试试看{ //===迭代JSON数据并生成关联数组====/ auto dataarray=jvalue.as_array(); //循环通过“data”对象 对于(int i=0;i<dataarray.size();i++) { 尝试 { auto data=dataarray[i]; auto dataobj=data.at(u(“staff”).as_object(); int键; STD::字符串值; //循环遍历“data”的每个对象 对于(auto iterinner=dataobj.cbegin();iterinner!=dataobj.cend();++iterinner) { auto&propertyname=iterinner->首先; auto&propertyValue=iterinner->秒; 如果(propertyname==l“_id”) { key=propertyValue.as ou integer(); } else if(propertyname==l“name”)。 { value=conversions::to_utf8string(propertyvalue.as_string()); } } StabdMpId.Enter(STD::MaMax对(密钥,值)); } 捕捉(const STD::异常和e) { STD:STD:WCTUT和LT; } } } catch(const STD::异常和e){ STD:STD:WCTUT和LT; } } PPLX::Tope& Lt:HtpPyRetry≫TaskSuffEnter(HtpHclient & Acto;Client,方法MTD,JSON::值const & jValst,STD::String String Type) { //STD::字符串URL=“/API/认证/搜索人员/”; STD::String URL =“/API/AudioTals/OLDGMS/StuffsIIDInNe//Prime: 返回client.request(mtd,utility::conversions::to_string_t(url)); } 无效的MaMug请求(HTTPHEclipse和客户端,方法MTD,JSON::值const & jValst,STD::String String Type) { 任务请求(client、mtd、jvalue、searchtext) .then([](http_响应响应) { if(response.status_code()==状态_codes::OK) { 返回response.extract_json(); } 从结果(json::value())返回pplx::task_; }) 。然后([](pplx::task<json::value>previoustask) { 尝试 { 显示_json(previoustask.get(),l“r:”); } catch(http_exception const&e) { STD:STD:WCTUT和LT; } }) .wait(); } INTHEXCHECHECUSTS(STD::String String Type) { //每次呼叫都清除staffmap staffmap.clear(); http_客户端(u(“http://52.68.13.154:3000”)); auto null value=json::value::null(); //STD::String SexChyToTe=转换::ToUutf8string(L) 发出请求(客户端,方法::get,空值,搜索文本); 返回员工ID; }

    这是我在clr项目中的代码(时间戳)。这就是我如何接受来自我的dll项目的数据并将其显示到用户界面的方法。

    string^input=searchbox->文本;
    
    STD::String SqlTeXTe= MSCL::IOP::MalSaluasas&Lt:STD::String & Gt;(输入);
    
    //清除搜索框中的每种类型的ListView项
    listview1->项->clear();
    
    Staff::SearchStaff(搜索文本);
    STD::MAP & LT;int,STD::String & Gt;Suffmap =工作人员::GETSuffMax();
    
    STD::MAP和LT;int,STD::String和Gt::迭代器ITER;
    对于(iter=staffmap.begin();iter!=staffmap.end();iter++)
    {
    string^value=msclr::interop::marshal_as<system::string^>(iter->秒);
    int key=iter->第一个;
    
    listviewitem=gcnew windows::forms::listviewitem(value);
    listViewItem->子项->添加(system::convert::toString(key));
    此->列表视图1->项->添加(此->列表视图项);
    }
    

    我希望它在ListView中正确显示名称和ID,但结果是:

    我希望有人能帮我解决这个问题。

    下面是场景: 这是从我的服务器返回的JSON数据,它是一个职员姓名列表,其中大部分是用日文汉字写的名字。

    [ 
      {   
         "staff": {
             "id": 121,
             "name": "福士 達哉",
             "department": [
                {
                   "_id": 3,
                   "name": "事業推進本部"
                }
             ]    
          } 
      },
      {   
         "staff": {
             "id": 12,
             "name": "北島 美奈",
             "department": [
                {
                   "_id": 4,
                   "name": "事業開発本部"
                }
             ]    
          } 
      },
      {   
         "staff": {
             "id": 151,
             "name": "大河原 紗希",
             "department": [
                {
                   "_id": 3,
                   "name": "事業推進本部"
                }
             ]    
          } 
      }
    ]
    

    这是我的dll项目(amscpsrest)中的代码。这是获取数据并传递到我的clr项目的方法:

    std::map<int, std::string> staffMap;
    auto GetStaffMap() -> std::map<int, std::string> {
        return staffMap;
    }
    
    void display_json(json::value const & jvalue, utility::string_t const & prefix)
    {
        try {
            //==== Iterate through json data and make an associative array ====/
            auto DataArray = jvalue.as_array();
    
            // loop through 'data' object
            for (int i = 0; i < DataArray.size(); i++)
            {
                try
                {
                    auto data = DataArray[i];
                    auto dataObj = data.at(U("staff")).as_object();
    
                    int key;
                    std::string value;
    
                    // loop through each object of 'data'
                    for (auto iterInner = dataObj.cbegin(); iterInner != dataObj.cend(); ++iterInner)
                    {
                        auto &propertyName = iterInner->first;
                        auto &propertyValue = iterInner->second;
    
                        if (propertyName == L"_id")
                        {
                            key = propertyValue.as_integer();
                        }
                        else if (propertyName == L"name")
                        {
                            value = conversions::to_utf8string(propertyValue.as_string());
                        }
                    }
                    staffMap.insert(std::make_pair(key, value));
                }
                catch (const std::exception& e)
                {
                    std::wcout << e.what() << std::endl;
                }
            }
    
        }
        catch (const std::exception& e) {
            std::wcout << e.what() << std::endl;
        }
    }
    
    pplx::task<http_response> task_request(http_client & client, method mtd, json::value const & jvalue, std::string searchText)
    {
        //std::string  url = "/api/authenticate/searchStaffs/";
        std::string url = "/api/authenticate/oldgms/staffs_id_name/";
    
        return client.request(mtd, utility::conversions::to_string_t(url));
    }
    
    void make_request(http_client & client, method mtd, json::value const & jvalue, std::string searchText)
    {
        task_request(client, mtd, jvalue, searchText)
            .then([](http_response response)
        {
            if (response.status_code() == status_codes::OK)
            {
                return response.extract_json();
            }
            return pplx::task_from_result(json::value());
        })
            .then([](pplx::task<json::value> previousTask)
        {
            try
            {
                display_json(previousTask.get(), L"R: ");
            }
            catch (http_exception const & e)
            {
                std::wcout << e.what() << std::endl;
            }
        })
            .wait();
    }
    
    
    int SearchStaff(std::string searchText)
    {
        //clear staffMap every call
        staffMap.clear();
    
        http_client client(U("http://52.68.13.154:3000"));
    
        auto nullValue = json::value::null();
        //std::string search_text = conversions::to_utf8string(L"北島 美奈");
        make_request(client, methods::GET, nullValue, searchText);
    
        return staff_id;
    }
    

    这是我在clr项目中的代码(时间戳)。这就是我如何接受来自我的dll项目的数据并显示到用户界面。

    String^ input = searchBox->Text;
    
    std::string searchText = msclr::interop::marshal_as<std::string>(input);
    
    // Clear listView item every type in searchbox
    listView1->Items->Clear();
    
    Staffs::SearchStaff(searchText);
    std::map<int, std::string> staffMap = Staffs::GetStaffMap();
    
    std::map<int, std::string>::iterator iter;
    for (iter = staffMap.begin(); iter != staffMap.end(); iter++)
    {
        String^ value = msclr::interop::marshal_as<System::String^>(iter->second);
        int key = iter->first;
    
        listViewItem = gcnew Windows::Forms::ListViewItem(value);
        listViewItem->SubItems->Add(System::Convert::ToString(key));
        this->listView1->Items->Add(this->listViewItem);
    }
    

    我希望它在ListView中正确显示名称和ID,但结果是: enter image description here

    我希望有人能帮我解决这个问题。

    1 回复  |  直到 8 年前
        1
  •  2
  •   p-a-o-l-o    8 年前

    _id id int key

    std::string UTF8Encoding (docs here)

    String^ value = msclr::interop::marshal_as<System::String^>(iter->second);
    

    //make a byte array to hold the string chars
    array<Byte>^ bytes = gcnew array<Byte>(iter->second.size());
    
    //copy the string chars into the byte array
    System::Runtime::InteropServices::Marshal::Copy(IntPtr(&iter->second[0]), bytes, 0, iter->second.size());
    
    //get a string from the bytes, using UTF8Encoding
    String^ value = System::Text::UTF8Encoding::UTF8->GetString(bytes);
    
    推荐文章