Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct usage of Poco C++ JSON for parsing data

Tags:

Can anyone instruct me on how the Poco C++ JSON works?

Previously I've used JsonReader and JsonToken. The Poco C++ library doesn't seem to have corresponding objects.

How do I for example use the json parser to create a object name consisting the JSON value at the tag name?

like image 511
David Karlsson Avatar asked Mar 13 '13 13:03

David Karlsson


People also ask

What is the use of JSON parsing?

JSON. parse() is a crucial method for converting JSON data in string form into Javascript objects. It is possible to convert simple or complex objects, but you should never convert calculations or code, like for loops.

Do I need to use JSON parse?

JSON as a string json or profiles. json above, that file actually contains text in the form of a JSON object or array, which happens to look like JavaScript. Just like with text files, if you want to use JSON in your project, you'll need to parse or change it into something your programming language can understand.

What is parsing in Android explain JSON parsing with suitable example?

Android App Development for Beginners JSON stands for JavaScript Object Notation.It is an independent data exchange format and is the best alternative for XML. This chapter explains how to parse the JSON file and extract necessary information from it. Android provides four different classes to manipulate JSON data.


1 Answers

EDIT: as of 1.5.2, things were simplified by making DefaultHandler, well ... default (and renaming it to its proper name - ParseHandler. So, if all you need is parsing, no need to explicitly provide the handler anymore:

// objects std::string json = "{ \"test\" : { \"property\" : \"value\" } }"; Parser parser; Var result = parser.parse(json); Object::Ptr object = result.extract<Object::Ptr>(); Var test = object->get("test"); object = test.extract<Object::Ptr>(); test = object->get("property"); std::string value = test.convert<std::string>();  // array of objects std::string json = "[ {\"test\" : 0}, { \"test1\" : [1, 2, 3], \"test2\" : 4 } ]"; Parser parser; Var result = parser.parse(json); Array::Ptr arr = result.extract<Array::Ptr>(); Object::Ptr object = arr->getObject(0);// assert (object->getValue<int>("test") == 0); object = arr->getObject(1); arr = object->getArray("test1"); result = arr->get(0); assert (result == 1); 

See this answer for more details.

like image 133
Alex Avatar answered Oct 19 '22 00:10

Alex