用C+处理汉字+; 我有一个用C++编写的Windows桌面应用程序(命名为:时间戳),使用.NET调用CLR.< /P> 我还使用C++编写了DLL项目(命名为AMSCPPRET),并使用CPPRESTSDK从服务器获取JSON数据,并将数据传递给我的时间戳应用程序。

用C+处理汉字+; 我有一个用C++编写的Windows桌面应用程序(命名为:时间戳),使用.NET调用CLR.< /P> 我还使用C++编写了DLL项目(命名为AMSCPPRET),并使用CPPRESTSDK从服务器获取JSON数据,并将数据传递给我的时间戳应用程序。,c++,clr,cpprest-sdk,cjk,kanji,C++,Clr,Cpprest Sdk,Cjk,Kanji,以下是场景: 这是从我的服务器返回的json数据,它是一个员工姓名列表,其中大部分是用汉字书写的日文姓名 [ { "staff": { "id": 121, "name": "福士 達哉", "department": [ { "_id": 3, "name": "事業推進本部" } ]

以下是场景: 这是从我的服务器返回的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项目(AMSCPREST)中的代码。以下是获取数据并传递到我的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;
}
std::map staffMap;
auto GetStaffMap()->std::map{
返回员工地图;
}
void display_json(json::value const&jvalue,utility::string_t const&prefix)
{
试一试{
//==遍历json数据并生成关联数组====/
auto DATARRAY=jvalue.as_array();
//循环遍历“数据”对象
对于(int i=0;ifirst;
auto&propertyValue=iterInner->second;
if(propertyName==L“\u id”)
{
key=propertyValue.as_integer();
}
else if(propertyName==L“名称”)
{
value=conversions::to_utf8string(propertyValue.as_string());
}
}
insert(std::make_pair(key,value));
}
捕获(const std::exception&e)
{
std::wcout子项->添加(系统::转换::ToString(键));
此->列表视图1->项目->添加(此->列表视图项目);
}
我希望它能在listview中正确显示名称和id,但结果是:


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

我想你们这里有两个截然不同的问题

首先,在迭代过程中,您尝试读取一个名为
\u id
的键,但它不在那里(它应该是
id
),因此您的
int键从来没有被赋值(而且它没有初始化,这就是为什么您在列表视图中会得到一个奇怪的数字)

其次,您必须将utf8(存储在
std::string
中)转换为ucs2,ucs2是.NET字符串的组成部分。您可以使用
UTF8Encoding
类来实现这一点。因此,代替此:

String^ value = msclr::interop::marshal_as<System::String^>(iter->second);
String^value=msclr::interop::marshal_as(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);
//创建一个字节数组来保存字符串字符
数组^bytes=gcnew数组(iter->second.size());
//将字符串字符复制到字节数组中
System::Runtime::InteropServices::Marshal::Copy(IntPtr(&iter->second[0]),字节,0,iter->second.size();
//使用UTF8编码从字节中获取字符串
String^value=System::Text::UTF8Encoding::UTF8->GetString(字节);

您在处理字符编码时存在不一致性。您的GUI最有可能期望它是UCS-16。JSON编码的是什么?汉字在
std::string=std::basic_string
中可能吗?还是您需要
std::wstring=std::basic_string
?您的
map
也可以是
map
甚至
map
你不需要一次又一次地编码和解码你的名字值。请看。@OstrichGroomer DLL项目是原生的C++,所以
String^
不存在,不是吗?不,不存在。使用
std::wstring
。是的,先生关于_id中的问题,我已经注意到并更改了它。当我试图运行此代码时出现错误。^:canno不要在类型“std::array”上使用此间接寻址;使用未定义的类型“std::array”您是否在顶部添加了一个
#include
?是的,先生。我添加了itI,现在我找到了此错误的解决方案。从这里开始,我只需在数组之前添加cli::如下所示:cli::array^字节,它就工作了。
//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);