Help in JSON format

I am using a JSON example from the web as shown below.

{ "menu": "File", "commands": [ { "title": "New", "action":"CreateDoc" }, { "title": "Open", "action": "OpenDoc" }, { "title": "Close", "action": "CloseDoc" } ] } 

I tried loading this in two different parsers: one in C ++ and Python.

Here's a Python trace.

 Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python2.6/json/__init__.py", line 267, in load parse_constant=parse_constant, **kw) File "/usr/lib/python2.6/json/__init__.py", line 307, in loads return _default_decoder.decode(s) File "/usr/lib/python2.6/json/decoder.py", line 319, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/usr/lib/python2.6/json/decoder.py", line 338, in raw_decode raise ValueError("No JSON object could be decoded") ValueError: No JSON object could be decoded 

And here is what jsoncpp reports.

 * Line 1, Column 1 Syntax error: value, object or array expected. 

Any clues what am I doing wrong?

Edit:

Ok, here is some code. For some reason, Python is working. I did nothing but go to the store. It should be a Python function - go to the store, random errors will disappear. These Python developers are geniuses.

But to the point. Here is the C ++ code.

 bool CFG::CFG_Init( const char* path ) { bool r = reader.parse( path, root ); if( r ) { return true; } else { std::cout << reader.getFormatedErrorMessages() << std::endl; return false; } } 

I tried this when the "path" was std :: string, and also the same. I call the method as follows:

 if( !CFG_Init("test.json") ) { error("Couldn't load configuration."); } 

And here is the class.

 class CFG: virtual Evaluator { Json::Reader reader; public: Json::Value root; bool CFG_Init( const char* path); Json::Value CFG_Fetch_Raw(Json::Value section, std::string key, Json::Value defval); Json::Value CFG_Fetch(Json::Value section, std::string key, Json::Value defval ); }; 
+6
c ++ json python
source share
3 answers

Ok, looking at jsoncpp code, I understand my mistake. He wants the document to be a string, not a file name.

+11
source share

Apparently your parser. I can correctly import a simplejson parser file into django

 >>> from django.utils import simplejson as sj >>> f=file("x.json") >>> sj.load(f) {u'menu': u'File', u'commands': [{u'action': u'CreateDoc', u'title': u'New'}, {u'action': u'OpenDoc', u'title': u'Open'}, {u'action': u'CloseDoc', u'title': u'Close'}]} >>> 
+1
source share

This JSON looks great. I would check the code that you use to download it to make sure that you download this file correctly and use the correct encoding to read the file from disk. Make sure you have no problems, such as trying to read a UTF-16 file as UTF-8 or trying to read lines with completed CRLF while waiting for lines or reading a file that starts with a specification with code that doesn't know how to skip it , or something like that. Take a look at the file in the hex editor to check for invisible characters that might throw things away.

+1
source share

All Articles