-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to feed a predefined integer value into json string #2138
Comments
Could it be that |
c and d is also initialized. The value of c and d are 110 and 130. |
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 So either you use 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 {"x_coordinate": a, "y_coordinate" : b, "width": c, "height": d} is not valid JSON. Instead, you will get the exception
|
To achieve my goal i have done the coding as below: } void read_json() Now its working perfectly. Thanx for your quick support. You can close this ticket now. |
The above code gives me the below exception error:
case 4:
JSON_THROW(static_cast<const detail::out_of_range>(&ex));
Any kind of help to solve this problem would be highly appreciated.
The text was updated successfully, but these errors were encountered: