ข้ามไปที่เนื้อหาหลัก

บทความ

กำลังแสดงโพสต์ที่มีป้ายกำกับ JSON

C++ JSONCPP

หลังจากทดลองใช่ ๋json_spirit (จากบทความ JSON Spirit ) มาซักพัก ผลคือใช้งานได้ดีทีเดียว แต่ตัว ่json_spirit เองก็ยังใช้งานซับซ้อนอยู่มากกว่าจะเข้าถึงข้อมูลได้ในแต่ละลำดับ ส่วน ๋JSONCPP นั้นมีการโอเวอร์โหลดโอเปอร์เรเตอร์ไว้หลายตัว ทำให้ใช้งานได้ง่ายขึ้น ทดลองเอา ตัวอย่างเดิมมาเขียนใหม่ #include <iostream> #include <string> using namespace std; #include <jsoncpp/json/reader.h> int main(){     std::string json_str = "{\"a man\":{\"name\":\"a man\",\"age\":25},\"a boy\":{\"name\":\"a boy\", \"age\":15}}";     cout << "The JSON string: " << json_str << endl;     Json::Value root;     Json::Reader reader;     cout<<"Read JSON string: "<<reader.parse(json_str, root)<<endl;     for(Json::ValueIterator i = root.begin(); i != root.end(); i++){         auto k...

JSON Spirit

พอเริ่มที่จะต้องแลกเปลี่ยนข้อมูลกันระหว่างโปรแกรม คำถามคือใช้อะไร แบบใหน โปรโตคอลอะไรดี สำหรับ Thesis ที่ทำขึ้นใหม่นี้ใช่เวลาไม่นานเพื่อหาคำตอบนี้ โดยความตั้งใจว่าจะใช้ JSON :D ปัญหาอยู่ที่ว่าไม่เคยเขียนโปรแกรมโดยใช้ JSON ด้วย C++ มาก่อน หลังจากหาข้อมูลอยู่พักใหญ่จึงไปตกลงใจที่ JSON Spirit ก่อนเริ่มเขียนโปรแกรมก็ควรติดตั้งไลบรารีกันก่อนนะครับ # aptitude install libjson-spirit-dev เสร็จสิ้นแล้วเริ่มเขียนโปรแกรมกันเลย ตัวอย่างแรก #include <iostream> #include <string> #include <vector> using namespace std; #include <json_spirit.h> int main (){ string json = "[1,2,3,4,5,6]"; cout << "The JSON string: " << json << endl; json_spirit::Value value; cout << "Read JSON string: " << json_spirit::read(json, value) << endl; std::vector< json_spirit::Value > array = value.get_array(); for( int i = 0; i < array.size(); i++) cout << "Get JSON value " << i << ...