目前分類:json (2)

瀏覽方式: 標題列表 簡短摘要
struct stat sb;
struct json_object *json, *obj;
char url[128], file[128], *buff;
int  fd=0, value=0;

snprintf(file, 127, "/tmp/%d_ipcam.json", (int)pthread_self());
snprintf(url, 127, "http://%s:%d/tmp/sapido/ipcam_schedule/%d.json", ip, port, index);
if (wget(url, file) == 0) {
    do {
        fd = open(file, O_RDONLY);
        if (fd < 0) continue;
        if (fstat(fd, &sb) < 0) continue;
        buff = (char*) mmap(0, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
        if (buff == MAP_FAILED) continue;
        json = json_tokener_parse(buff);
        if (json != NULL) {
            obj = json_object_object_get(json, "format");
            if (obj != NULL) value = json_object_get_int(obj);
            json_object_put(json);
        }
        munmap(buff, sb.st_size);
    } while (0);
    if (fd) close(fd);
}
printf("value: %d\n", value);
return value;

iterator for json

#include 
#include 
#include 

int main(int argc, char *argv[])
{
        struct json_object_iterator it;
        struct json_object_iterator itEnd;
        struct json_object *obj, *value;

        obj = json_tokener_parse("{'first':'george', 'second':'', 'third':null, 'age':100, }");
        it = json_object_iter_begin(obj);
        itEnd = json_object_iter_end(obj);

        while (!json_object_iter_equal(&it, &itEnd)) {
                value = json_object_iter_peek_value(&it);
                if (json_object_get_type(value) == json_type_string) {
                        printf("%s: %s\n",
                                json_object_iter_peek_name(&it),
                                json_object_get_string(value));
                }
                else if (json_object_get_type(value) == json_type_int) {
                        printf("%s: %d\n",
                                json_object_iter_peek_name(&it),
                                json_object_get_int(value));
                }
                else {
                        printf("%s: %d\n",
                                json_object_iter_peek_name(&it),
                                json_object_get_type(value));
                }
                json_object_iter_next(&it);
        }
        json_object_put(obj);
}
# ./json-itor
first: george
second:
third: 0
age: 100

台南小新 發表在 痞客邦 留言(0) 人氣()

json-c wiki: https://github.com/json-c/json-c/wiki

json-c download: https://s3.amazonaws.com/json-c_releases/releases/index.html

台南小新 發表在 痞客邦 留言(0) 人氣()