MCPcopy Create free account
hub / github.com/danielaparker/jsoncons / get_example

Function get_example

examples/src/examples.cpp:237–274  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

235}
236
237void get_example()
238{
239 json j = json::parse(R"(
240 {
241 "application": "hiking",
242 "reputons": [
243 {
244 "rater": "HikingAsylum",
245 "assertion": "advanced",
246 "rated": "Marilyn C",
247 "rating": 0.90
248 }
249 ]
250 }
251 )");
252
253 // Using index or `at` accessors
254 std::string result1 = j["reputons"][0]["rated"].as<std::string>();
255 std::cout << "(1) " << result1 << '\n';
256 std::string result2 = j.at("reputons").at(0).at("rated").as<std::string>();
257 std::cout << "(2) " << result2 << '\n';
258
259 // Using JSON Pointer
260 std::string result3 = jsonpointer::get(j, "/reputons/0/rated").as<std::string>();
261 std::cout << "(3) " << result3 << '\n';
262
263 // Using JSONPath
264 json result4 = jsonpath::json_query(j, "$.reputons.0.rated");
265 if (result4.size() > 0)
266 {
267 std::cout << "(4) " << result4[0].as<std::string>() << '\n';
268 }
269 json result5 = jsonpath::json_query(j, "$..0.rated");
270 if (result5.size() > 0)
271 {
272 std::cout << "(5) " << result5[0].as<std::string>() << '\n';
273 }
274}
275
276int main()
277{

Callers 1

mainFunction · 0.85

Calls 5

parseFunction · 0.85
json_queryFunction · 0.85
atMethod · 0.80
getFunction · 0.50
sizeMethod · 0.45

Tested by

no test coverage detected