1

I was trying my hands with C++ REST APIs I wrote to json using below way.

json::value resp;
std::vector<Portfolio> portfolio;
// Populate portfolio
this->PortfolioList(usrStr, pwdStr, portfolio);
std::vector<Portfolio>::iterator it;
for (it = portfolio.begin(); it != portfolio.end(); it++)
{
    char costBuff[40]; _itoa_s(it->GetTotalCost(), costBuff, 10);
    char qtyBuff[40]; _itoa_s(it->GetQuantity(), qtyBuff, 10);

    json::value portfolioEntry;
    portfolioEntry[U("username")] = json::value::string(utility::conversions::to_string_t(it->GetUserName()));
    portfolioEntry[U("stockCode")] = json::value::string(utility::conversions::to_string_t(it->GetStockCode()));
    portfolioEntry[U("quantity")] = json::value::string(utility::conversions::to_string_t(qtyBuff));
    portfolioEntry[U("totalcost")] = json::value::string(utility::conversions::to_string_t(costBuff));

    resp[utility::conversions::to_string_t(it->GetStockCode())] = portfolioEntry;
}

For this I got output as below

{
 "11002":{"quantity":11002,"totalcost":"272","username":"arunavk"},
 "11003":{"quantity":11003,"totalcost":"18700","username":"arunavk"},
 "11004":{"quantity":11004,"totalcost":"760","username":"arunavk"},
 "11005":{"quantity":11005,"totalcost":"32","username":"arunavk"}
}

Now, on the receiving end, I tried to read it as below

for (int i = 0; i < size; i++)
{
    table->elementAt(i, 0)->addWidget(new Wt::WText(this->response[i][0].as_string()));
    table->elementAt(i, 1)->addWidget(new Wt::WText(this->response[i][1].as_string()));
    table->elementAt(i, 2)->addWidget(new Wt::WText(this->response[i][2].as_string()));
    table->elementAt(i, 3)->addWidget(new Wt::WText(this->response[i][3].as_string()));
} 

But it fails. What am I missing ?

Pardon me, I am new to this REST and Casablanca and JSON

SimpleGuy
  • 2,764
  • 5
  • 28
  • 45

1 Answers1

2

From JSON point of view the following

{
 "11002":{"quantity":11002,"totalcost":"272","username":"arunavk"},
 "11003":{"quantity":11003,"totalcost":"18700","username":"arunavk"},
 "11004":{"quantity":11004,"totalcost":"760","username":"arunavk"},
 "11005":{"quantity":11005,"totalcost":"32","username":"arunavk"}
}

is java script object with properties "11002", ... "11005", is not array. So if you want to get value of property you have to use property name:

this->response["11002"]["quantity"]

because when you use integer index the json::value::operator [] supposes that you want to access the array element. Here is details https://microsoft.github.io/cpprestsdk/classweb_1_1json_1_1value.html#a56c751a1c22d14b85b7f41a724100e22

UPDATED

If you do not know properties of the recieved object, you can call the value::as_object method (https://microsoft.github.io/cpprestsdk/classweb_1_1json_1_1value.html#a732030bdee11c2f054299a0fb148df0e) to get JSON object and after that you can use specilized interface to iterate through the fields with begin and end iterators: https://microsoft.github.io/cpprestsdk/classweb_1_1json_1_1object.html#details

AnatolyS
  • 4,249
  • 18
  • 28
  • And what if the receiver does not know the `"11002"` as these are the stockCodes which he wants and has prior no information ? – SimpleGuy Aug 11 '16 at 01:21