In-depth exploration of JSON, XML and Conf file formats and C language parsing methods

Introduction:
In software development, it is very important to choose a suitable file format to store and transmit data. Common file formats are JSON, XML, and Conf files. This article will introduce the characteristics, advantages and disadvantages of these three file formats in detail, and provide methods and examples for parsing in C language.

1. JSON

  1. Introduction to JSON:
    JSON (JavaScript Object Notation) is a lightweight data exchange format widely used in web development. It organizes data in key-value pairs and uses curly and square brackets to denote objects and arrays. JSON supports a variety of data types such as strings, numbers, booleans, arrays, and objects. JSON has the characteristics of easy reading and writing, good cross-platform compatibility, and high transmission efficiency.

  2. JSON parsing library:
    In C language, we have two common JSON parsing libraries to choose from:

  • json-c: This is a powerful JSON parsing library that provides the ability to parse and generate JSON data. It has good compatibility and stability, and is suitable for processing complex JSON data structures.
  • cJSON: This is a lightweight JSON parsing library that only includes two source code files. It is suitable for handling simple JSON data.
  1. Parsing method:
    Here is an example of parsing a JSON file using json-cthe library:
#include <stdio.h>
#include <json-c/json.h>

int main()
{
    
    
    const char *json_string = "{\"name\": \"John\", \"age\": 30, \"city\": \"New York\"}";

    json_object *root = json_tokener_parse(json_string);
    if (root == NULL) {
    
    
        printf("JSON解析错误\n");
        return 1;
    }

    json_object *name, *age, *city;
    json_object_object_get_ex(root, "name", &name);
    json_object_object_get_ex(root, "age", &age);
    json_object_object_get_ex(root, "city", &city);

    printf("Name: %s\n", json_object_get_string(name));
    printf("Age: %d\n", json_object_get_int(age));
    printf("City: %s\n", json_object_get_string(city));

    json_object_put(root);

    return 0;
}

advantage:

  • The JSON format is easy to read, write and understand, and is friendly to humans.
  • It has good cross-platform compatibility and supports most programming languages.
  • High transmission efficiency, small data volume, suitable for network transmission.

shortcoming:

  • It is not suitable for storing a large amount of complex data, because the JSON file will be longer and take up more storage space.

Two, XML

  1. Introduction to XML:
    XML (Extensible Markup Language) is a markup language used to describe and transmit structured data. It uses tags to define elements and attributes, and supports custom tags and structures. XML has the advantages of clear structure, data independence and object representation, and is suitable for storing and exchanging data.

  2. XML parsing library:
    In C language, we can use libxml2library to parse and manipulate XML files. libxml2It is a powerful XML parsing library that provides multiple parsing methods, such as DOM (Document Object Model) and SAX (Simple API for XML).

  • DOM parsing method: load the entire XML document into memory and represent it in a tree structure. DOM has an intuitive operation interface and is suitable for processing small XML documents.
  • SAX parsing method: Parse XML documents line by line in an event-driven manner without loading the entire document into memory. SAX is suitable for processing large XML documents, but the operation is relatively complicated.
  1. Parsing method:
    The following is an example of using libxml2the library to parse an XML file (using the DOM method):
#include <stdio.h>
#include <libxml/parser.h>

void parseXML(const char *filename)
{
    
    
    xmlDocPtr doc;
    xmlNodePtr root, node;

    doc = xmlParseFile(filename);
    if (doc == NULL) {
    
    
        printf("XML解析错误:%s\n", filename);
        return;
    }

    root = xmlDocGetRootElement(doc);
    if (root == NULL) {
    
    
        printf("空白XML文档:%s\n", filename);
        xmlFreeDoc(doc);
        return;
    }

    for (node = root->children; node != NULL; node = node->next) {
    
    
        if (node->type == XML_ELEMENT_NODE) {
    
    
            printf("节点名:%s\n", node->name);
        }
    }

    xmlFreeDoc(doc);
}

int main()
{
    
    
    const char *filename = "data.xml";
    parseXML(filename);

    return 0;
}

advantage:

  • The structure is clear and easy to understand and maintain.
  • Supports custom labels and strong scalability.
  • Suitable for storing and exchanging data.

shortcoming:

  • The file is large and occupies a high bandwidth during network transmission.

3. Conf file

  1. Introduction to Conf files:
    Conf files are used to store configuration information of applications or systems, usually in the form of key-value pairs, and control the behavior of applications in simple text configurations. Conf files are easy to use and lightweight, and are suitable for storing simple configuration information.

  2. Parsing method:
    Using C language to parse Conf files is mainly based on basic file reading operations. We can read the Conf file line by line and parse out key-value pairs to obtain configuration information.

Here is a simple example:

#include <stdio.h>
#include <stdlib.h>

void parseConf(const char *filename)
{
    
    
    FILE *fp;
    char line[100];
    char key[50], value[50];

    fp = fopen(filename, "r");
    if (fp == NULL) {
    
    
        printf("无法打开配置文件:%s\n", filename);
        return;
    }

    while (fgets(line, sizeof(line), fp)) {
    
    
        if (line[0] != '#' && sscanf(line, "%s %s", key, value) == 2) {
    
    
            printf("键:%s,值:%s\n", key, value);
        }
    }

    fclose(fp);
}

int main()
{
    
    
    const char *filename = "config.conf";
    parseConf(filename);

    return 0;
}

advantage:

  • Simple and easy to use, suitable for storing simple configuration information.
  • Lightweight, small file size.

shortcoming:

  • Not suitable for storing complex structures and large amounts of data.

Summary:
In software development, choosing a suitable file format and parsing method is crucial for data storage and transmission. JSON is famous for its simplicity and clarity, cross-platform compatibility, and efficient data transmission; XML is widely used for its clear structure, data independence, and object representation; Conf files are suitable for simple storage because of their ease of use and light weight. configuration information. The above is a supplementary answer about the detailed introduction of these three file formats and their parsing methods in C language, as well as their advantages and disadvantages. By choosing an appropriate file format and parsing method, we can improve development efficiency and data interaction quality.

Guess you like

Origin blog.csdn.net/qq_37037348/article/details/132182370