数据交互格式

1、概述

  1. 自定义结构
  2. XML
  3. JSON
  4. Protocol Buffers

2、自定义结构-简单

// 定义一个表示学生的结构体

struct Student {    

string name;    

int age;    

float gpa;

};

3、自定义结构-复杂

#include <iostream>

#include <string>

#include <vector>

// 定义学生结构体

struct Student {    

std::string name;    

int age;    

float gpa;

};

// 定义课程结构体

struct Course {    

std::string courseName;    

std::string instructor;    

std::vector<Student> students;

};

// 定义大学结构体

struct University {    

std::string name;    

std::vector<Student> students;    

std::vector<Course> courses;

};

4、XML-简单

<Student ID="123">

  <Name>Alice</Name>

  <Age>20</Age>

  <GPA>3.8</GPA>

</Student>

5、XML-复杂

        <University>

            <Name>ABC University</Name>

            <Students>

                <Student>

                    <Name>Alice</Name>

                    <Age>20</Age>

                    <GPA>3.8</GPA>

                </Student>

                <Student>

                    <Name>Bob</Name>

                    <Age>19</Age>

                    <GPA>3.5</GPA>

                </Student>

            </Students>

            <Courses>

                <Course>

                    <CourseName>Math</CourseName>

                    <Instructor>Dr. Smith</Instructor>

                </Course>

                <Course>

                    <CourseName>Physics</CourseName>

                    <Instructor>Dr. Johnson</Instructor>

                </Course>

            </Courses>

        </University>

6、JSON-简单

{

  "name": "Alice",

  "age": 20,

  "gpa": 3.8

}

7、JSON-复杂

{

            "University": {

                "Name": "ABC University",

                "Students": [

                    {

                        "Name": "Alice",

                        "Age": 20,

                        "GPA": 3.8

                    },

                    {

                        "Name": "Bob",

                        "Age": 19,

                        "GPA": 3.5

                    }

                ],

                "Courses": [

                    {

                        "CourseName": "Math",

                        "Instructor": "Dr. Smith"

                    },

                    {

                        "CourseName": "Physics",

                        "Instructor": "Dr. Johnson"

                    }

                ]

            }

        }

8、Protocol Buffers-简单

syntax = "proto3";

message Student {

  string name = 1;

  int32 age = 2;

  float gpa = 3;

}

9、Protocol Buffers-复杂

syntax = "proto3";

message Student {

  string name = 1;

  int32 age = 2;

 gpa = 3;

}

message Course {

  string course_name = 1;

  string instructor = 2;

  repeated Student students = 3;

}

message University {

  string name = 1;

  repeated Student students = 2;

  repeated Course courses = 3;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值