0
web::json::value obj;

obj[JSONKeyRequest] = web::json::value::string(JSONValueRequest);

I create a JSON value, and insert some key and values to it. Then I get this obj in another function, trying to check that whether obj[JSONKeyRequest] equals to "abc", but it doesn't work:

web::json::value getObj = this->GetSendObj();
if (getObj[JSONKeyRequest] == web::json::value::string(L"abc"))
{
}

However, VC keeps shows:"Errors:No operators [] matches this operands , operand types are const web::json::value[std::wstring] So, how could I get the value based on the key and compare the value with a string ?

firstaccount
  • 155
  • 2
  • 13

2 Answers2

0

First of all, the error tells you what exactly the arguments should be.

json requires wstring arguments, or if not wstring, you can input literals.

obj[L"JSONKeyRequest"] = web::json::value(L"JSONValueRequest");

should work.

Secondly, for comparison try declaring a wstring first and then comparing like

wstring temp = "abc";
if (getObj["JSONKeyRequest"] == temp)
{
}

This should work.

  • JSONKeyRequest is a macro defines a string. The error happens at getObj[JSONKeyRequest] , it points out the error happens to the first brace [ , not because the right side of the equal sign. – firstaccount Sep 30 '16 at 21:48
0
if (getObj.at(key) == web::json::value::string(L"abc"))

This one works for me.

firstaccount
  • 155
  • 2
  • 13