永发信息网

c++ json解析字符串

答案:2  悬赏:20  手机版
解决时间 2021-03-07 07:49
  • 提问者网友:原来太熟悉了会陌生
  • 2021-03-06 09:00
C++ 中解析该字符串的方法???
最佳答案
  • 五星知识达人网友:罪歌
  • 2021-03-06 09:25
[{"att":0,"cross":-1,"lon":120.27253050430015,"lat":36.273210578019814},{"att":1,"cross":1,"lon":120.27261376990259,"lat":36.273210933950786},{"att":2,"cross":1,"lon":120.27269982411042,"lat":36.273199944344164}]
全部回答
  • 1楼网友:时间的尘埃
  • 2021-03-06 09:43
一、从字符串中读取json #include #include "json/json.h" using namespace std; int main() { //字符串 const char * str = "{\"praenomen\":\"gaius\",\"nomen\":\"julius\",\"cognomen\":\"caezar\"," "\"born\":-100,\"died\":-44}" ; json::reader reader; json::value root; //从字符串中读取数据 if (reader.parse(str,root)) { string praenomen = root[ "praenomen" ].asstring(); string nomen = root[ "nomen" ].asstring(); string cognomen = root[ "cognomen" ].asstring(); int born = root[ "born" ].asint(); int died = root[ "died" ].asint(); cout << praenomen + " " + nomen + " " + cognomen << " was born in year " << born << ", died in year " << died << endl; } return 0; } makefile文件 lib=-l /usr/local/lib/libjson/ -ljson_linux- gcc -4.4.7_libmt a: a.o g++ -o a -std=c++0x a.o $(lib) a.o: a.cpp g++ -c a.cpp clean: rm -rf a.o a 二、从文件中读取json personalinfo.json(一个存储了json格式字符串的文件) { "name" : "tsybius" , "age" :23, "sex_is_male" : true , "partner" : { "partner_name" : "galatea" , "partner_age" :21, "partner_sex_is_male" : false }, "achievement" :[ "ach1" , "ach2" , "ach3" ] } #include #include #include "json/json.h" using namespace std; int main() { json::reader reader; json::value root; //从文件中读取 ifstream is; is.open( "personalinfo.json" , ios::binary); if (reader.parse(is,root)) { //读取根节点信息 string name = root[ "name" ].asstring(); int age = root[ "age" ].asint(); bool sex_is_male = root[ "sex_is_male" ].asbool(); cout << "my name is " << name << endl; cout << "i'm " << age << " years old" << endl; cout << "i'm a " << (sex_is_male ? "man" : "woman" ) << endl; //读取子节点信息 string partner_name = root[ "partner" ][ "partner_name"].asstring(); int partner_age = root[ "partner" ][ "partner_age" ].asint(); bool partner_sex_is_male = root[ "partner" ]["partner_sex_is_male" ].asbool(); cout << "my partner's name is " << partner_name << endl; cout << (partner_sex_is_male ? "he" : "she" ) << " is " << partner_age << " years old" << endl; //读取数组信息 cout << "here's my achievements:" << endl; for ( int i = 0; i < root[ "achievement" ].size(); i++) { string ach = root[ "achievement" ][i].asstring(); cout << ach << '\t' ; } cout << endl; cout << "reading complete!" << endl; } is.close(); return 0; } makefile ? lib=-l /usr/local/lib/libjson/ -ljson_linux- gcc -4.4.7_libmt a: a.o g++ -o a -std=c++0x a.o $(lib) a.o: a.cpp g++ -c a.cpp clean: rm -rf a.o a 三、将信息保存为json格式 a.cpp #include #include #include "json/json.h" using namespace std; int main() { //根节点 json::value root; //根节点属性 root[ "name" ] = json::value( "tsybius" ); root[ "age" ] = json::value(23); root[ "sex_is_male" ] = json::value( true ); //子节点 json::value partner; //子节点属性 partner[ "partner_name" ] = json::value( "galatea" ); partner[ "partner_age" ] = json::value(21); partner[ "partner_sex_is_male" ] = json::value( false ); //子节点挂到根节点上 root[ "partner" ] = json::value(partner); //数组形式 root[ "achievement" ].append( "ach1" ); root[ "achievement" ].append( "ach2" ); root[ "achievement" ].append( "ach3" ); //直接输出 cout << "fastwriter:" << endl; json::fastwriter fw; cout << fw.write(root) << endl << endl; //缩进输出 cout << "styledwriter:" << endl; json::styledwriter sw; cout << sw.write(root) << endl << endl; //输出到文件 ofstream os; os.open( "personalinfo" ); os << sw.write(root); os.close(); return 0; } makefile lib=-l /usr/local/lib/libjson/ -ljson_linux- gcc -4.4.7_libmt a: a.o g++ -o a -std=c++0x a.o $(lib) a.o: a.cpp g++ -c a.cpp clean: rm -rf a.o a { "achievement" : [ "ach1" , "ach2" , "ach3" ], "age" : 23, "name" : "tsybius" , "partner" : { "partner_age" : 21, "partner_name" : "galatea" , "partner_sex_is_male" : false }, "sex_is_male" : true }
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯