0.o数据解析测试

This commit is contained in:
sjm 2024-06-10 13:30:40 +08:00
parent dea8ea58e4
commit 53b888b24b
3 changed files with 71 additions and 1 deletions

View File

@ -1,15 +1,77 @@
#define SDL_MAIN_HANDLED #define SDL_MAIN_HANDLED
#include <iostream> #include <iostream>
#include <fstream>
#include <sstream>
#include <cJSON.h>
#include <SDL.h> #include <SDL.h>
#include <SDL_ttf.h> #include <SDL_ttf.h>
#include <SDL_mixer.h> #include <SDL_mixer.h>
#include <SDL_image.h> #include <SDL_image.h>
#include <SDL2_gfxPrimitives.h> #include <SDL2_gfxPrimitives.h>
void test_json()
{
std::ifstream file("test.json");
if (!file.good())
{
std::cout << "无法打开文件" << std::endl;
return;
}
std::stringstream str_stream;
str_stream << file.rdbuf();
file.close();
cJSON* json_root = cJSON_Parse(str_stream.str().c_str());
cJSON* json_name = cJSON_GetObjectItem(json_root, "name");
cJSON* json_age = cJSON_GetObjectItem(json_root, "age");
cJSON* json_pets = cJSON_GetObjectItem(json_root, "pets");
std::cout << json_name->string << ":" << json_name->valuestring << std::endl;
std::cout << json_age->string << ":" << json_age->valueint << std::endl;
std::cout << json_pets->string << ":" << std::endl;
cJSON* json_item = nullptr;
cJSON_ArrayForEach(json_item, json_pets)
{
std::cout << "\t" << json_item->valuestring << std::endl;
}
}
void test_csv()
{
std::ifstream file("test.csv");
if (!file.good())
{
std::cout << "无法打开文件" << std::endl;
return;
}
std::string str_line;
while (std::getline(file, str_line))
{
std::string str_grid;
std::stringstream str_stream(str_line);
while (std::getline(str_stream, str_grid, ','))
{
std::cout << str_grid << std::endl;
}
std::cout << std::endl;
}
file.close();
}
int main() int main()
{ {
test_json();
std::cout << "============================" << std::endl;
test_csv();
SDL_Init(SDL_INIT_EVERYTHING); SDL_Init(SDL_INIT_EVERYTHING);
IMG_Init(IMG_INIT_JPG | IMG_INIT_PNG); IMG_Init(IMG_INIT_JPG | IMG_INIT_PNG);
@ -18,7 +80,7 @@ int main()
Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048); Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048);
//创建窗口和与之关联渲染器 //创建窗口和与之关联渲染器
SDL_Window* window = SDL_CreateWindow(u8"ÄãºÃ£¬ÊÀ½ç ", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, SDL_WINDOW_SHOWN); SDL_Window* window = SDL_CreateWindow(u8"ÄãºÃ£¬ÊÀ½ç ", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, SDL_WINDOW_SHOWN);
SDL_Renderer* renderer = SDL_CreateRenderer(window,-1,SDL_RENDERER_ACCELERATED); SDL_Renderer* renderer = SDL_CreateRenderer(window,-1,SDL_RENDERER_ACCELERATED);

3
Demo/test.csv Normal file
View File

@ -0,0 +1,3 @@
1 2 3
4 5 6
7 8 9
1 1 2 3
2 4 5 6
3 7 8 9

5
Demo/test.json Normal file
View File

@ -0,0 +1,5 @@
{
"name": "XiaoMing",
"age": 18,
"pets": ["dog","cat","bird"]
}