Overview
Features
Download
Documentation
Community
Add-Ons & Services

Poco c++ JSON useage

Please post support and help requests here.

Poco c++ JSON useage

Postby devdavid » 13 Mar 2013, 16:36

Can any body instruct me on how the Poco c++ JSON works? http://pocoproject.org/docs-1.5.0/Poco.JSON.html

Previously i've used JsonReader: http://developer.android.com/reference/ ... eader.html and JsonToken

The Pococ++ library doesnt seem to have correspodning objects. How do i for example use the json parser to create a object name consisting the json value at the tag name?

Am i supposed to write a class inheriting the handler class containing if methods and such for parsing out parts of the json?

I want multiple objects to be created on parsing, for example LocationObject, NameObject...

Best Regards
devdavid
 
Posts: 17
Joined: 11 Mar 2013, 16:10

Re: Poco c++ JSON useage

Postby rakesh » 13 Mar 2013, 23:13

Take a look at the JSON/testsuite/src/JSONTest.cpp file in the source tree. It is fairly simple higher level DOM style API than the SAX style API you linked to.
rakesh
 
Posts: 78
Joined: 13 Apr 2011, 17:43
Location: Chicago

Re: Poco c++ JSON useage

Postby alex » 14 Mar 2013, 23:30

I would not agree that Poco::JSON is DOM. It is actually SAX-ish in it's core because even the parsing is done through DefaultHandler (probably not the best name). You can write custom SAX-like handlers for various functionality, I'm currrently working on a pretty-print one.

Here's the example for the OP:

Code: Select all
std::string json = "{ \"test\" : { \"property\" : \"value\" } }";
Parser parser;
DefaultHandler handler;
parser.setHandler(&handler);
parser.parse(json);
Var result = handler.result();
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>();
alex
 
Posts: 1130
Joined: 11 Jul 2006, 16:27
Location: United_States

Re: Poco c++ JSON useage

Postby devdavid » 18 Mar 2013, 10:24

Thank you so much, I try to parse:

"{\"Store\":[ {\"test\" : \"firststring\"}, { \"test1\" : [1, 2, 3], \"test2\" : 4 } ]}"
This throws me an error.

i actually need to parse something with more hierarchy levels like : "{\"RefList\":{\"Ref\":[ {\"test\" : \"firststring\"}, { \"test1\" : [1, 2, 3], \"test2\" : 4 } ]}}". Again error.

Is there a bug or something when more levels are added?

Thank You so much!
devdavid
 
Posts: 17
Joined: 11 Mar 2013, 16:10

Re: Poco c++ JSON useage

Postby alex » 18 Mar 2013, 13:34

What kind of error? Your examples parse fine:
Code: Select all
std::string json = "{\"RefList\":{\"Ref\":[ {\"test\" : \"firststring\"}, { \"test1\" : [1, 2, 3], \"test2\" : 4 } ]}}";
Parser parser;
Var result;
ParseHandler handler;
parser.setHandler(&handler);
parser.parse(json);
result = handler.result();
assert(result.type() == typeid(Object::Ptr));
alex
 
Posts: 1130
Joined: 11 Jul 2006, 16:27
Location: United_States

Re: Poco c++ JSON useage

Postby devdavid » 18 Mar 2013, 15:09

Hi,
It seems that the line throwing an exception is the last one in:

Code: Select all
    std::string json = "{\"RefList\":{\"Ref\":[ {\"test\" : \"firststring\"}, { \"test1\" : [1, 2, 3], \"test2\" : 4 } ]}}";
    Parser parser;
    DefaultHandler handler;
    parser.setHandler(&handler);
    parser.parse(json);
    Var result = handler.result();
    RefListComplexType::Refs = result.extract<Array::Ptr>();



Can't the hierarchy be extracted in this way?

The exception message in XCode is:
libc++abi.dylib: terminate called throwing an exception
devdavid
 
Posts: 17
Joined: 11 Mar 2013, 16:10

Re: Poco c++ JSON useage

Postby alex » 18 Mar 2013, 15:43

Code: Select all
RefListComplexType::Refs = result.extract<Array::Ptr>();

You are trying to extract array but your JSON is an object.
alex
 
Posts: 1130
Joined: 11 Jul 2006, 16:27
Location: United_States


Return to Support

Who is online

Users browsing this forum: No registered users and 1 guest

cron