Я использую библиотеку C Янссон http://www.digip.org/jansson/Извлечение Янссон данных JSON
Это довольно простой в использовании https://jansson.readthedocs.org/en/2.7/tutorial.html#the-program
Но я не могу получить простой int
из моей JSON строки. Я могу успешно получить и load
строку JSON (как у меня нет ошибок, ничего нет null
), но когда я использую функции jansson get
для получения int
, мой int
всегда равен 0, хотя с помощью шагов и точек останова функция jansson обрабатывать ин является не возвращения 0.
строка JSON выглядит следующим образом:
{"type":3}
Вот код:
static void foo(json_t *jsonRoot) {
// json root is error checked even before this, and is not null
if (jsonRoot == NULL) {
return;
}
// Trying to get type = 3
json_t *j_type;
int type = 0;
j_type = json_object_get(jsonRoot, "type");
if (!json_is_integer(j_type)) {
printf("Not an int!\n");
return;
} else {
// I get in to the else
// json_integer_value has a its own internal check and
// will return 0 if the value is not an int, but it is not
// returning 0. It is running the macro json_to_integer(json)->value
type = json_integer_value(j_type);
}
printf("type is %d\n", type);
// type is 0
}
это нормально работает. попробуйте на стороне вызывающего абонента 'char text [] =" {\ "type \": 3} "; \t Ошибка json_error_t; \t json_t * корень; \t root = json_loads (текст, 0, & ошибка); \t if (! Root) { \t fprintf (stderr, "error: root \ n"); \t \t fprintf (stderr, "error: on line% d:% s \ n", error.line, error.text); \t \t выход (1); \t} \t foo (корень); ' – BLUEPIXY
Пробовал это, все равно получаю 0. Идти орехи! – BarryBones41