int a,b,c,d;
a = 80;
b = 90;
std::string s = "{\"x_coordinate\": a, \"y_coordinate\" : b, \"width\": c, \"height\": d}";
json j = json::parse(s);
The above code gives me the below exception error:
case 4:
JSON_THROW(*static_cast
Any kind of help to solve this problem would be highly appreciated.
Could it be that c and d are uninitialized?
c and d is also initialized. The value of c and d are 110 and 130.
if i use directly the value in json string like:
std::string s = "{"x_coordinate": 80, "y_coordinate" : 90, "width": 110, "height": 130}"; then its working okay.
But for my application, i need to feed the integer value into json string.
What is your actual code? Parsing the string
std::string s = "{\"x_coordinate\": a, \"y_coordinate\" : b, \"width\": c, \"height\": d}";
seems not what you are doing, because then you would get a parse error after key x_coordinate.
So either you use std::to_string to put your integers into the string, or you create the value programmatically:
json j;
j["x_coordinate"] = a;
j["y_coordinate"] = b;
j["width"] = c;
j["height"] = c;
Here is my actual code:
void create_json()
{
std::string s = "{\"x_coordinate\": a, \"y_coordinate\" : b, \"width\": c, \"height\": d}";
json j = json::parse(s);
for (const auto& element : j["x_coordinate"])
{
std::cout << element << std::endl;
}
}
void showImage()
{
image = img.clone();
rectangle(image, cropRect, Scalar(0, 255, 0), 2, 8, 0);
imshow(winName, image);
}
int main(int argc, char** argv)
{
int a,b,c,d;
img = imread("C:/output/aaa.png", IMREAD_COLOR);
while (1)
{
showImage();
char c = waitKey() % 256;
if (c == '6') cropRect.x++;
if (c == '4') cropRect.x--;
printf("The value of x_coordinate = %d\n", cropRect.x);
if (c == '8') cropRect.y++;
if (c == '2') cropRect.y--;
printf("The value of y_coordinate = %d\n", cropRect.y);
if (c == 'w') { cropRect.y--; cropRect.height++; }
if (c == 'd') cropRect.width++;
if (c == 'x') cropRect.height++;
if (c == 'a') { cropRect.x--; cropRect.width++; }
if (c == 't') { cropRect.y++; cropRect.height--; }
if (c == 'h') cropRect.width--;
if (c == 'b') cropRect.height--;
if (c == 'f') { cropRect.x++; cropRect.width--; }
printf("The value of width = %d\n", cropRect.width);
printf("The value of height = %d\n", cropRect.height);
if (c == 27) break;
if (c == 'r') { cropRect.x = 40; cropRect.y = 40; cropRect.width = 40; cropRect.height = 40; }
a = cropRect.x;
b = cropRect.y;
c = cropRect.width;
d = cropRect.height;
create_json();
}
return 0;
}
The value of cropRect.x, cropRect.y, cropRect.width, cropRect.height is changing from the keyboard event of OpenCV. The above 4 integer values are fed into new integer a, b, c, d. After that i am creating a json string and trying to parse it so that i can use the json string parameters to fill the one structure for further image processing.
Executing your create_json() function cannot work, as
{"x_coordinate": a, "y_coordinate" : b, "width": c, "height": d}
is not valid JSON.
Instead, you will get the exception
[json.exception.parse_error.101] parse error at line 1, column 18: syntax error while parsing value - invalid literal; last read: '"x_coordinate": a'
To achieve my goal i have done the coding as below:
int a=80;
int b=120;
int e = 110;
int d= 130;
void create_json()
{
json jsonfile;
jsonfile["x_coordinate"] = a;
jsonfile["y_coordinate"] = b;
jsonfile["width"] = e;
jsonfile["height"] = d;
std::ofstream file("key.json");
file << jsonfile;
}
void read_json()
{
std::ifstream ifs("key.json");
json j = json::parse(ifs);
for (const auto& element : j["x_coordinate"])
{
std::cout<<"x_coordinate: " << element << std::endl;
}
for (const auto& element : j["y_coordinate"])
{
std::cout << "y_coordinate: " << element << std::endl;
}
for (const auto& element : j["width"])
{
std::cout << "width: " << element << std::endl;
}
for (const auto& element : j["height"])
{
std::cout << "height: " << element << std::endl;
}
}
Now its working perfectly. Thanx for your quick support. You can close this ticket now.