|
| 1 | +/* |
| 2 | + * ===================================================================================== |
| 3 | + * |
| 4 | + * Filename: json_test.cpp |
| 5 | + * |
| 6 | + * Description: |
| 7 | + * |
| 8 | + * Version: 1.0 |
| 9 | + * Created: 2017/02/15 16时44分43秒 |
| 10 | + * Revision: none |
| 11 | + * Compiler: gcc |
| 12 | + * |
| 13 | + * Author: Chen Xi (happyleaf), happyleaf.cx |
| 14 | + * Organization: |
| 15 | + * |
| 16 | + * ===================================================================================== |
| 17 | + */ |
| 18 | + |
| 19 | + |
| 20 | +#include <stdio.h> |
| 21 | +#include <stdlib.h> |
| 22 | +#include <json/json.h> |
| 23 | +/* |
| 24 | + * === FUNCTION ====================================================================== |
| 25 | + * Name: main |
| 26 | + * Description: |
| 27 | + * ===================================================================================== |
| 28 | + */ |
| 29 | +#include <iostream> |
| 30 | +#include <string> |
| 31 | +#include <fstream> |
| 32 | +#include <assert.h> |
| 33 | +using namespace std; |
| 34 | + |
| 35 | +int main(int argc, char *argv[]) { |
| 36 | + char *json = " \ |
| 37 | +{ \n\ |
| 38 | + \"encoding\" : \"UTF-8\", \n\ |
| 39 | + \n\ |
| 40 | + \"plug-ins\" : [ \n\ |
| 41 | + \"python\", \n\ |
| 42 | + \"c++\", \n\ |
| 43 | + \"ruby\" \n\ |
| 44 | + ], \n\ |
| 45 | + \n\ |
| 46 | + \"indent\" : { \"length\" : 3, \"use_space\": true } \n\ |
| 47 | +}\n"; |
| 48 | + Json::Value root(json); |
| 49 | + cout<<root.asString()<<endl; |
| 50 | +// cout<<(root.get("encoding","UTF-8").asString())<<endl; |
| 51 | +// string a = root["encoding"].asString(); |
| 52 | + Json::Reader reader; |
| 53 | + |
| 54 | + if (!reader.parse(json, json + strlen(json), root, false)) { |
| 55 | + return -1; |
| 56 | + } |
| 57 | + cout<<(root.get("encoding","UTF-7").asString())<<endl; |
| 58 | + string a = root["encoding"].asString(); |
| 59 | + root["big"] = "new"; |
| 60 | + Json::Value child = root; |
| 61 | + root["child"] = child; |
| 62 | + Json::StyledWriter sw; |
| 63 | + cout<<sw.write(root)<<endl; |
| 64 | + |
| 65 | + //read from file |
| 66 | + ifstream ifs; |
| 67 | + ifs.open("testjson.json"); |
| 68 | + assert(ifs.is_open()); |
| 69 | + Json::Value root2; |
| 70 | + if (!reader.parse(ifs, root2, false)) |
| 71 | + { |
| 72 | + return -1; |
| 73 | + } |
| 74 | + string name = root2["name"].asString(); |
| 75 | + cout<<"parse from file ,name is["<<name<<"]"<<endl; |
| 76 | + Json::Value age = root2.get("age", 2); |
| 77 | +// Json::Value age = root.["age"].asInt() |
| 78 | + cout<<"parse from file ,age is["<<age.asInt()<<"]"<<endl; |
| 79 | + cout << root2["name"].asString()<<endl; |
| 80 | + root2["aaa"]="222"; |
| 81 | + cout << root2["aaa"].asString()<<endl; |
| 82 | + cout<<sw.write(root2)<<endl; |
| 83 | + |
| 84 | +// root2["new"] = "hello world!"; |
| 85 | + |
| 86 | + return EXIT_SUCCESS; |
| 87 | +} /* ---------- end of function main ---------- */ |
0 commit comments