示例#1
0
    void OnData(
        const happyhttp::Response* r,
        void* userdata,
        const unsigned char* data,
        int n
        )
    {
        std::cout << "reading...\n";

        char *endptr;
        JsonValue value;
        JsonAllocator allocator;
        JsonParseStatus status = jsonParse((char*)data, &endptr, &value, allocator);
        if (status != JSON_PARSE_OK) {
            fprintf(stderr, "error at %zd, status: %d\n", endptr - (char*)data, status);
            exit(EXIT_FAILURE);
        } else { // SUCCESS
            switch (value.getTag()) {
            case JSON_TAG_NUMBER:
                printf("%g\n", value.toNumber());
                break;
            case JSON_TAG_BOOL:
                printf("%s\n", value.toBool() ? "true" : "false");
                break;
            case JSON_TAG_STRING:
                printf("\"%s\"\n", value.toString());
                break;
            case JSON_TAG_ARRAY:
                for (auto i : value) {
                    auto bleh = i->value;
                }
                break;
            case JSON_TAG_OBJECT:
                for (auto i : value) {
                    printf("%s = ", i->key);
                }
                break;
            case JSON_TAG_NULL:
                printf("null\n");
                break;
            }
        }
    }
示例#2
0
void NewsScene::printReturn(JsonValue o)
{
    switch (o.getTag())
    {
        case JSON_TAG_NUMBER:
            printf("%g\n", o.toNumber());
            //sum += o.toNumber();
            break;
        case JSON_TAG_BOOL:
            printf("%s\n", o.toBool() ? "true" : "false");
            break;
        case JSON_TAG_STRING:
        {
            string theValue =o.toString();
            setValue(theValue);
        }
            break;
        case JSON_TAG_ARRAY:
            for (auto i : o)
            {
               // tmpNews = new News();
                listNews.push_back(new News());
                printReturn(i->value);
            }
            
            break;
        case JSON_TAG_OBJECT:
            for (auto i : o)
            {
                printf("%s = ", i->key);
                lastKey = i->key;
                printReturn(i->value);
            }
            break;
        case JSON_TAG_NULL:
            printf("null\n");
            break;
    }
}