JSON library in C

下载地址:

git clone https://github.com/kgabis/parson.git

Examples:

void print_commits_info(const char *username, const char *repo) {
    JSON_Value *root_value;
    JSON_Array *commits;
    JSON_Object *commit;
    size_t i;
    
    char curl_command[512];
    char cleanup_command[256];
    char output_filename[] = "commits.json";
    
    /* it ain't pretty, but it's not a libcurl tutorial */
    sprintf(curl_command, 
        "curl -s \"https://api.github.com/repos/%s/%s/commits\" > %s",
        username, repo, output_filename);
    sprintf(cleanup_command, "rm -f %s", output_filename);
    system(curl_command);
    
    /* parsing json and validating output */
    root_value = json_parse_file(output_filename);
    if (json_value_get_type(root_value) != JSONArray) {
        system(cleanup_command);
        return;
    }
    
    /* getting array from root value and printing commit info */
    commits = json_value_get_array(root_value);
    printf("%-10.10s %-10.10s %s\n", "Date", "SHA", "Author");
    for (i = 0; i < json_array_get_count(commits); i++) {
        commit = json_array_get_object(commits, i);
        printf("%.10s %.10s %s\n",
               json_object_dotget_string(commit, "commit.author.date"),
               json_object_get_string(commit, "sha"),
               json_object_dotget_string(commit, "commit.author.name"));
    }
    
    /* cleanup code */
    json_value_free(root_value);
    system(cleanup_command);
}

Calling ```print_commits_info("torvalds", "linux");``` prints:  
```
Date       SHA        Author
2012-10-15 dd8e8c4a2c David Rientjes
2012-10-15 3ce9e53e78 Michal Marek
2012-10-14 29bb4cc5e0 Randy Dunlap
2012-10-15 325adeb55e Ralf Baechle
2012-10-14 68687c842c Russell King
2012-10-14 ddffeb8c4d Linus Torvalds
 

### Persistence
In this example I'm using parson to save user information to a file and then load it and validate later.
```c
void persistence_example(void) {
    JSON_Value *schema = json_parse_string("{\"name\":\"\"}");
    JSON_Value *user_data = json_parse_file("user_data.json");
    char buf[256];
    const char *name = NULL;
    if (user_data == NULL || json_validate(schema, user_data) != JSONSuccess) {
        puts("Enter your name:");
        scanf("%s", buf);
        user_data = json_value_init_object();
        json_object_set_string(json_object(user_data), "name", buf);
        json_serialize_to_file(user_data, "user_data.json");
    }
    name = json_object_get_string(json_object(user_data), "name");
    printf("Hello, %s.", name);
    json_value_free(schema);
    json_value_free(user_data);
    return;
}
```

### Serialization
Creating JSON values is very simple thanks to the dot notation. 
Object hierarchy is automatically created when addressing specific fields. 
In the following example I create a simple JSON value containing basic information about a person.
```c
void serialization_example(void) {
    JSON_Value *root_value = json_value_init_object();
    JSON_Object *root_object = json_value_get_object(root_value);
    char *serialized_string = NULL;
    json_object_set_string(root_object, "name", "John Smith");
    json_object_set_number(root_object, "age", 25);
    json_object_dotset_string(root_object, "address.city", "Cupertino");
    json_object_dotset_value(root_object, "contact.emails", json_parse_string("[\"email@example.com\",\"email2@example.com\"]"));
    serialized_string = json_serialize_to_string_pretty(root_value);
    puts(serialized_string);
    json_free_serialized_string(serialized_string);
    json_value_free(root_value);
}

```

Output:
```
{
    "name": "John Smith",
    "age": 25,
    "address": {
        "city": "Cupertino"
    },
    "contact": {
        "emails": [
            "email@example.com",
            "email2@example.com"
        ]
    }
}

 

创建JSON
    root_value = json_value_init_object();
    root_object = json_value_get_object(root_value);

    json_object_dotset_string(root_object, APP_POWER_PERCENT, powervalue);
        
    serialized_string = json_serialize_to_string_pretty(root_value);
    json_size = json_serialization_size_pretty(root_value);
解析payload
    jsonStringHandle = STRING_from_byte_array(payLoad, size));
  
    jsonString = STRING_c_str(jsonStringHandle);
 
    rootValue = json_parse_string(jsonString);
   

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vetur is a VS Code extension for Vue.js development. It requires a `tsconfig.json` or `jsconfig.json` file to provide configuration information for TypeScript or JavaScript respectively. If you don't already have a `tsconfig.json` or `jsconfig.json` file in your project directory, you can create one by running `tsc --init` or `npm init` respectively. If you do have one of these files in your project directory, make sure that it is located in the root directory of your project (`c:\MyReponsitory\library-management-system-web_1`), and that its name is spelled correctly (`tsconfig.json` or `jsconfig.json`). If the file is located in a different directory or has a different name, you can specify its location in your VS Code settings. Open your VS Code `settings.json` file and add the following line: ``` "vetur.validation.template": false, "vetur.useWorkspaceDependencies": true, "vetur.experimental.templateInterpolationService": true, "vetur.format.defaultFormatterOptions": { "prettier": { "singleQuote": true, "semi": false } }, "vetur.format.defaultFormatter": { "prettier": { "useTabs": true, "tabWidth": 2, "singleQuote": true, "semi": false } }, "vetur.validation.interpolation": false, "vetur.validation.script": false, "vetur.validation.style": false, "vetur.ignoreProjectWarning": true, "vetur.format.options.tabSize": 2, "vetur.format.options.useTabs": false, "vetur.format.scriptInitialIndent": false, "vetur.format.styleInitialIndent": false, "vetur.format.defaultFormatter.js": "prettier", "vetur.format.defaultFormatter.html": "prettier", "vetur.format.defaultFormatter.css": "prettier", "vetur.format.defaultFormatter.scss": "prettier", "vetur.format.defaultFormatter.less": "prettier", "vetur.format.defaultFormatter.postcss": "prettier", "vetur.validation.templateProps": false, "vetur.validation.interpolation": false, "vetur.format.defaultFormatterOptions": { "prettier": { "singleQuote": true, "semi": false } }, "vetur.format.enable": true, "vetur.validation.template": false, "vetur.useWorkspaceDependencies": true, "vetur.experimental.templateInterpolationService": true, "vetur.format.defaultFormatterOptions": { "prettier": { "singleQuote": true, "semi": false } }, "vetur.format.defaultFormatter": { "prettier": { "useTabs": true, "tabWidth": 2, "singleQuote": true, "semi": false } }, "vetur.validation.interpolation": false, "vetur.validation.script": false, "vetur.validation.style": false, "vetur.ignoreProjectWarning": true, "vetur.format.options.tabSize": 2, "vetur.format.options.useTabs": false, "vetur.format.scriptInitialIndent": false, "vetur.format.styleInitialIndent": false, "vetur.format.defaultFormatter.js": "prettier", "vetur.format.defaultFormatter.html": "prettier", "vetur.format.defaultFormatter.css": "prettier", "vetur.format.defaultFormatter.scss": "prettier", "vetur.format.defaultFormatter.less": "prettier", "vetur.format.defaultFormatter.postcss": "prettier", "[vue]": { "editor.defaultFormatter": "octref.vetur" } ``` In the `"vetur.validation.template"` section, add the following line: ``` "vetur.validation.template": false, ``` This will disable template validation in Vetur, which may be causing the error message you are seeing. I hope this helps! Let me know if you have any further questions.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值