- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有一个用 C++ 编写的 Windows 桌面应用程序(名为:Timestamp),它使用名为 CLR 的 .NET。
我还有一个用 native c++ 编写的 DLL 项目(名为:Amscpprest),并使用 CPPREST SDK 从服务器获取 json 数据并将数据传递到我的时间戳应用程序。
场景如下:这是从我的服务器返回的 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 项目 (Amscpprest) 中的代码。这是获取数据并传递到我的 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,但这是结果:
我希望有人能帮我解决这个问题。
最佳答案
我认为您这里有两个截然不同的问题。
首先,在迭代中,您尝试读取一个名为 _id
的键,但它不存在(它应该是 id
),因此您的 int key
从未被分配一个值(并且它没有被初始化,这就是为什么你在 ListView 中得到一个奇怪的数字)。
其次,您必须将 utf8(存储在 std::string
中)转换为 ucs2,这是 .NET 字符串的组成部分。您可以使用 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);
关于c++ - 在 C++ 中处理汉字字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51204412/
我在 Eclipse 3.4.1 中有一个项目,我想在其中使用日语符号(如象形文字)。问题是这些符号无法在 Eclipse 中正确显示,我看到的是黑框而不是它们。 我对所有文件使用“Utf-8”编码。
我有一个 C++ 项目应该添加 到每一行的开头和到每一行的末尾。这适用于普通英文文本,但我有一个中文文本文件,我想这样做,但它不起作用。我通常使用 .txt 文件,但为此我必须使用 .rtf 来保存中
我正在尝试对中文文本进行文本分析。该程序在下面提供。我得到了带有不可读字符的结果,例如 滨烘暯镞ユ姤捐。如果我将输出文件 result.csv 更改为 result.txt,字符将正确为 人民日报社论
我有这段代码,但它只适用于英文字符 $( "input[name*='Name']" ).attr("placeholder","姓名"); 我的网页可以很好地显示其他中文字符,如果我将中文字符更改为
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎偏离主题,因为它缺乏足够的信息来诊断问题。 更详细地描述您的问题或 include a mini
在 C# 窗口应用程序中,当我这样做时 select * from myTable where category = '牛奶' 或 select * from myTable where catego
我的数据库中有英文和中文字符,但是当我在 JSON 中检索时,中文字符不起作用。结果是“?”。有人可以帮助我吗? 0, "message" => 'Wrong user id'); } else {
Image to show the problem这是说明问题的代码: # -*- coding:utf-8 -*- text = u"严" print text 如果我在 VSCode 调试中运行上
美好的一天。 我在linux下直接打印带有汉字的文件时遇到问题。 我们使用CUPS来管理Linux上的打印机并通过a2ps发送打印命令。 我们的文件采用encode/unicode(UTF-8和ISO
效果如图所示: Default.aspx 复制代码代码如下: <table> <tr> <td class="s
我正在尝试将两个 double 值转换为 C++ 中的字符串,以便我可以使用 TRACE 在我的输出窗口中查看它们。我正在使用 Visual Studio 2017。这是我的主要代码: double
我正在尝试读取输入并仅在它是日文汉字时才传递它。根据http://www.rikai.com/library/kanjitables/kanji_codes.unicode.shtml,这意味着 An
我需要在数据库中插入汉字,但它总是显示??? .. 示例: 插入此记录。 微波室外单元-Apple 然后就变成了??? 结果: ??????-苹果 我真的需要帮助...谢谢您的关心。 我使用的是 MS
我熟悉不同的 MySQL 排序规则,我的数据库当前设置为 UTF8_general_ci,并且我使用 UTF8_unicode_ci 进行了半成功的搜索。我正在运行 mysql 5.6,我无法使用最新
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 4 年前。 Improv
我是一名优秀的程序员,十分优秀!