1

任何人都知道基于 C 的 JSON 解析器,它允许我访问用于数字字段的字符串表示形式?我的意思的例子:

json_t * dist = json_object_get(coord, "accuracy");
snprintf(dataOut->distance, MAX_COORD_LEN, "%f", json_number_value(dist));

这是愚蠢且难以编写单元测试的。我宁愿简单地调用json_string_value(dist)并准确获取该号码的字符串。我更希望它不会费心将该字符串转换为数字。这样,当我为我的例程提供一个包含“54.6045”的测试字符串时,我会得到“54.6045”而不是一些填充或舍入的值......而且我不需要解析这个数字,因为我永远不会去将其用作一个。

据我所知,没有这样的事情......在我看来这很愚蠢。上面的示例来自 Jansson,使用字符串值函数返回 null。

我真的不想因为这个而不必自己写。

4

4 回答 4

2

In case you didn't already know this, http://www.json.org/ maintains a list of JSON libraries written in a wide variety of languages, including 12 of them in C.

I'm using jsmn for a resource-constrained, embedded platform. All it does is tokenize JSON strings, so you could get what you need from that library, but you would need to construct a little more logic around it to do anything useful.

Similarly, you could probably adapt JSON_checker to do what you want.

If you can't find one that does what you want, it's not that hard to parse yourself.

于 2012-05-22T20:58:56.340 回答
0

您可以使用https://github.com/cesanta/frozen

自述文件中有一个示例,可以准确显示您的要求,这是一个简短版本:

static const char *str = " { foo: { bar: [ 80, 443 ], baz: 1.2e-21 } } ";
struct json_token tokens[50];
int size = sizeof(tokens) / sizeof(tokens[0]);
const struct json_token *tok = NULL;

parse_json(str, strlen(str), tokens, size);
tok = find_json_token(tokens, "foo.bar[1]");
printf("My number is: [%.*s]\n", tok->len, tok->ptr)
于 2013-12-28T13:25:28.440 回答
0

Tiny-json 是一个 C 解析器,它允许您以最简单的方式访问数字字段的字符串表示形式。只需请求一个 JSON 属性,它就会返回一个指向以空值结尾的指针。 https://github.com/rafagafe/tiny-json

#include <stdio.h>
#include "tiny-json.h"

int main( int argc, char** argv ) {

    char str[] = "{\"accuracy\":654,\"real\":2.54e-3}";

    // In the example only 3 is needed. For root and two fields.
    json_t mem[ 3 ];

    json_t const* root = json_create( str, mem, sizeof mem / sizeof *mem );
    if ( !root ) {
        fputs( "Cannot create a JSON.\n", stderr );
        return 1;
    }

    { // You can get the primitive values as text format:

        char const* accuracy = json_getPropertyValue( root, "accuracy" );
        if ( !accuracy ) {
            fputs( "The filed 'accuracy' is not found.\n", stderr );
            return 1;
        }
        printf( "The accuracy value: %s\n", accuracy );

        char const* real = json_getPropertyValue( root, "real" );
        if ( !real ) {
            fputs( "The filed 'real' is not found.\n", stderr );
            return 1;
        }
        printf( "The real value: %s\n", real );

    }

    { // You can check the type of a field and get its value in binary format:

        json_t const* accuracy = json_getProperty( root, "accuracy" );
        if ( !accuracy ) {
            fputs( "The filed 'accuracy' is not found.\n", stderr );
            return 1;
        }
        if( json_getType( accuracy ) != JSON_INTEGER ) {
            fputs( "The filed 'accuracy' is not an integer.\n", stderr );
            return 1;
        }
        // Get the value in binary format:
        long long accuracyVal = json_getInteger( accuracy );
        printf( "The accuracy value: %lld\n", accuracyVal );
        // Get the value in text format:
        char const* accuracyTxt = json_getValue( accuracy );
        printf( "The accuracy value: %s\n", accuracyTxt );

        json_t const* real = json_getProperty( root, "real" );
        if ( !accuracy ) {
            fputs( "The filed 'real' is not found.\n", stderr );
            return 1;
        }
        if( json_getType( real ) != JSON_REAL ) {
            fputs( "The filed 'real' is not a real.\n", stderr );
            return 1;
        }
        // Get the value in binary format:
        double realVal = json_getReal( real );
        printf( "The real value: %f\n", realVal );
        // Get the value in text format:
        char const* realTxt = json_getValue( real );
        printf( "The real value: %s\n", realTxt );

    }

    return 0;
}
于 2017-04-23T21:44:45.367 回答
-2

Jansson 是一个很棒的库,如果基于单元测试选择另一个库,那就太可惜了。我不确定你为什么反对解析数值——你能解释一下吗?

就您的代码片段而言,您可能会对从结构中检索数值所涉及的样板文件感到有些恼火。我建议你学会爱上 json_pack / json_unpack 函数。您可以从字典中检索整数元素,如下所示:

double d;
json_unpack(coord, "{s:f}", "accuracy", &d);

使用此调用,您不必使用表示浮点值的 (json_t *) 对象。当然,您应该检查 json_unpack() 调用的返回码以了解错误情况。

于 2012-05-22T21:22:38.310 回答