Source Code File 12-01_types
12-01_types.c
#include <stdio.h>
#include <stdlib.h>
#include <json-c/json.h>
int main()
{
const char filename[] = "sample.json";
const char object_name[] = "assistant";
json_object *jdata,*objname;
enum json_type type;
jdata = json_object_from_file(filename);
if( jdata==NULL )
{
fprintf(stderr,"Unable to process %s\n",filename);
exit(1);
}
if( !json_object_object_get_ex(jdata,object_name,&objname))
{
fprintf(stderr,"Unable to find object %s\n",
object_name);
exit(1);
}
printf("The object type of '%s' is ",object_name);
type = json_object_get_type(objname);
switch(type)
{
case json_type_array:
puts("array");
break;
case json_type_boolean:
puts("boolean");
break;
case json_type_double:
puts("double");
break;
case json_type_int:
puts("integer");
break;
case json_type_null:
puts("null");
break;
case json_type_object:
puts("object");
break;
case json_type_string:
puts("string");
break;
default:
puts("unrecognized");
}
return(0);
}
Output
The object type of 'assistant' is string
Copyright © 1997-2025 by QPBC.
All rights reserved
