reading params from list in string


Hey everyone,

I have the following string:


 
 Reply from server: {"success":true,"uses":1,"purchase":{"seller_id":"PqK9YWSyICYZnR6hSnzzWA==","product_id":"yxuWCzDi8lqE0AlvvE_J3A==","product_name":"TestSoft",


Now I need to be able to get the success boolean and the uses into an integer, how would i go about this?
This looks like JSON. You might want to use a JSON library such as https://github.com/nlohmann/json
Last edited on
If all that is required are the 2 values for success and uses, then consider something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>
#include <sstream>

int main() {
	const std::string reply {R"({"success":true, "uses" : 1, "purchase" : {"seller_id":"PqK9YWSyICYZnR6hSnzzWA == ", "product_id" : "yxuWCzDi8lqE0AlvvE_J3A == ", "product_name" : "TestSoft")"};

	std::istringstream iss(reply);
	std::string succ, use;
	int uses {};

	std::getline(iss, succ, ':');
	std::getline(iss, succ, ',');
	std::getline(iss, use, ':');
	iss >> uses;

	std::cout << succ << "  " << uses << '\n';
}


which displays:


true  1

ow that is perfect thank you so much!!
seeplus' code assumes "success" is listed first and that there is a colon semicolon after the value.
Last edited on
Yes - if the format of the reply string changes, then that extraction code may not work. It is specific to the format shown. But it is simple and does avoid using a 3rd party JSON parser.

NB it's a colon not a semicolon!
Topic archived. No new replies allowed.