Skip to content
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

整数转换的结果,打印显示 ld #35

Open
DebGith opened this issue Jul 13, 2023 · 2 comments
Open

整数转换的结果,打印显示 ld #35

DebGith opened this issue Jul 13, 2023 · 2 comments

Comments

@DebGith
Copy link

DebGith commented Jul 13, 2023

我将 字符串 和 整数 转换成 json格式,然后用串口发送出去,字符串转换没问题,但整数转换得到的结果,用串口打印 %d 正常,用串口打印 %s 却显示 ld,不知道什么原因造成,而我需要发送的json数据,要能正常显示整数值。
请问如何解决
`

typedef struct
{
char TestString[10];
int TestInt;
}Struct_test;

cJSON struct_to_json_Struct_test(void struct_obj)
{
s2j_create_json_obj(json_obj_);
Struct_test *struct_obj_ = (Struct_test *)struct_obj;
s2j_json_set_basic_element(json_obj_, struct_obj_, string, TestString);
s2j_json_set_basic_element(json_obj_, struct_obj_, int, TestInt);
return json_obj_;
}

void json_to_struct_Struct_test(cJSON json_obj)
{
s2j_create_struct_obj(struct_obj_, Struct_test);
s2j_struct_get_basic_element(struct_obj_,json_obj, string, TestString);
s2j_struct_get_basic_element(struct_obj_,json_obj, int, TestInt);
return struct_obj_;
}

int main(void)
{
static Struct_test _Struct_test =
{
.TestString = "OK",
.TestInt = 123
};

cJSON *p1 = struct_to_json_Struct_test(&_Struct_test);
if(p1 != NULL)
{
	char *p2 = cJSON_Print(p1);
	if(p2 != NULL)
	{
		UartPrint("\nThe Json is:\n%s",p2);

		Struct_test *converted_obj = json_to_struct_Struct_test(p1);

		if (memcmp(&_Struct_test, converted_obj, sizeof(Struct_test)))
		{
			UartPrint("\nConverted failed!\n");
		}
		else
		{
			UartPrint("\nConverted OK!\n");

			UartPrint("converted_obj->TestString = %s\n", converted_obj->TestString);
			UartPrint("converted_obj->TestInt = %d\n", converted_obj->TestInt);
		}			
	}
}

return 0;

} `

运行结果:
The Json is:
{
"TestString": "OK",
"TestInt": ld
}
Converted OK!
converted_obj->TestString = OK
converted_obj->TestInt = 123

@DebGith
Copy link
Author

DebGith commented Jul 13, 2023

我期望的输出结果是:
{
"TestString": "OK",
"TestInt": 123
}
@armink 请教大佬

@DebGith
Copy link
Author

DebGith commented Jul 13, 2023

@yuxuebao 您知道吗

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant