JSON file in mbed.
Dependencies: MbedJSONValue mbed SDFileSystem
main.cpp
- Committer:
- AlexVC97
- Date:
- 2017-03-29
- Revision:
- 4:54fdacd71dc9
- Parent:
- 3:0ace19f0cc2b
File content as of revision 4:54fdacd71dc9:
#include "mbed.h" #include "MbedJSONValue.h" #include "SDFileSystem.h" #include "mission.h" #include <iostream> #include <vector> using namespace VivesCityGame; SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // mosi, miso, sclk, cs #define charlimit 100 char words[charlimit]; int n = 0, c; int main() { MbedJSONValue demo; FILE * fp1 = fopen("/sd/test.json", "r"); if(fp1 == NULL) { error("Could not open file for read!\r\n"); } printf("Reading from SD card...\r\n"); while((c = fgetc(fp1)) && c!=EOF) { words[n] = c; n++; } printf("Read from SD card: %s\r\n", words); fclose(fp1); //const char * json = "{\"gameMode\":\"LocationGame\",\"gameModeId\":1,\"version\":\"1.235\",\"creationDate\":\"11-02-2017\",\"missions\":[{\"type\":\"gotoLocation\",\"id\":1,\"message\":\"Go to the big roundabout in Oostende.\",\"deadline\":true,\"deadlineTime\":30,\"latitude\":51.21543,\"longitude\":2.928656,\"radius\":200},{\"type\":\"puzzle\",\"id\":2,\"message\":\"Find the key A\",\"hint\":\"Look in a tree\",\"deadline\":false}]}"; const char * json = words; //const char * json = "{\"my_array\": [\"demo_string\", 10], \"my_boolean\": true}"; // parse the previous string and fill the object demo parse(demo, json); printf(">>> testing if it works properly \r\n"); std::vector <Mission> missions; // declares an object of the class std::vector for(std::size_t i = 0; i < demo["missions"].size(); i++) { Mission mission; // declares a local variable of the class Mission mission.from_json(demo["missions"][i]); missions.push_back(mission); } cout << "Missions: " << endl; for(std::size_t i = 0; i < missions.size(); i++) { printf("Type: %s\r\n", missions[i].get_type().c_str()); printf("Id: %d\r\n", missions[i].get_id()); printf("Message: %s\r\n", missions[i].get_message().c_str()); printf("Hint: %s\r\n", missions[i].get_hint().c_str()); printf("Deadline: %s\r\n", missions[i].get_deadline() ? "true" : "false"); printf("DeadlineTime: %d\r\n", missions[i].get_deadlineTime()); printf("Latitude: %f\r\n", missions[i].get_latitude()); printf("Longitude: %f\r\n", missions[i].get_longitude()); printf("Radius: %d\r\n", missions[i].get_radius()); } /* for(std::size_t i = 0; i < missions.size(); i++) { cout << missions[i].to_string(); } */ /* std::string game_mode; int game_mode_id; std::string my_ver; std::string creation_date; */ /* game_mode = demo["gameMode"].get<std::string>(); game_mode_id = demo["gameModeId"].get<int>(); my_ver = demo["version"].get<std::string>(); creation_date = demo["creationDate"].get<std::string>(); */ /*for(int i = 0; i < 2; i++) { type = demo["missions"][i]["type"].get<std::string>(); type = demo["missions"][i]["id"].get<int>(); type = demo["missions"][i]["message"].get<std::string>(); type = demo["missions"][i]["hint"].get<std::string>(); type = demo["missions"][i]["deadline"].get<bool>(); type = demo["missions"][i]["deadlineTime"].get<int>(); type = demo["missions"][i]["altitude"].get<double>(); type = demo["missions"][i]["longitude"].get<double>(); type = demo["missions"][i]["radius"].get<int>(); } */ /* printf("game_mode: %s\r\n", game_mode.c_str()); printf("game_mode_id: %d\r\n", game_mode_id); printf("my_ver: %s\r\n", my_ver.c_str()); printf("creation_date: %s\r\n", creation_date.c_str()); printf("lat: %f\r\n", latitude); printf("long: %f\r\n", longitude); printf("rad: %d\r\n", rad); */ }