Coding Global Background
Coding Global

Can someone help me with my json reading via c++

Archived 2 years ago
4 messages
0 members
2 years ago
Open in Discord
M
mrj0632

I am currently working on a code snippet designed to extract the 'discordusername' field from a JSON dataset based on a provided IP address. The code is functional in many cases; however, I have encountered scenarios where the extraction fails, particularly when the JSON structure changes in size. The current implementation below, searches for the target dataset based on the provided IP address and attempts to extract the 'discordusername' field: std::wstring extractDiscordUsernameByIP(const std::wstring& jsonData, const std::wstring& ipAddress) { std::wstring ipAddressKey = L"\"ipaddress\":\"" + ipAddress + L"\""; size_t ipAddressPos = jsonData.find(ipAddressKey); if (ipAddressPos != std::wstring::npos) { size_t usernamePos = jsonData.rfind(L"\"discordusername\":\"", ipAddressPos); if (usernamePos != std::wstring::npos) { size_t startQuotePos = usernamePos + 18; // length of "\"discordusername\":\"" size_t endQuotePos = jsonData.find(L"\"", startQuotePos); if (endQuotePos != std::wstring::npos) { std::wstring extractedUsername = jsonData.substr(startQuotePos, endQuotePos - startQuotePos); std::wstring trimmedUsername = trim(extractedUsername); if (trimmedUsername.empty()) { return L""; } else { return trimmedUsername; } } } } return L""; } for some reason everytime it just provides an empty discord username, im geussing its having trouble finding it, i have also tried to add error logging and it usually is just having problems finding it, if anyone can help me with this please respond!

Replies (4)