24840

Reading JSON from a file using C++ REST SDK (Casablanca)

I have the following code which should read the contents of a text file and parse it as JSON

try { string_t importFile = argv[++iArgCounter]; // extract filename ifstream_t f(importFile); // filestream of working file stringstream_t s; // string stream for holding JSON read from file json::value v; // JSON read from input file iArgCounter++; // increment arg counter if (f) { s << f.rdbuf(); // stream results of reading from file stream into string stream f.close(); // close the filestream v.parse(s); // parse the resultant string stream. } } catch (web::json::json_exception excep) { std::cout << "ERROR Parsing JSON: "; std::cout << excep.what(); break; }

And the following test JSON file

[ { "Destinations": [ { "Domain": "127.0.0.1", "Name": "GoogleLogin", "Port": "8090" } ], "Listeners": [ { "Domain": "127.0.0.1", "Name": "LoginRequest", "Port": "8080", "Route": "ProcessLoginRequest" } ], "Name": "LoginProcess", "Routes": [ { "Name": "ProcessLoginRequest", "Rules": [{ "DestinationIfTrue": "GoogleLogin", "LeftTerm": { "RuleTermType": 1, "Value": "NETWORK" }, "Operator": 2, "RightTerm": { "RuleTermType": 0, "Value": "NETWORK" } }], "Transformations": [] } ] } ]

The trouble is no matter what the JSON code I get the error 'Line 1, Column 2 Syntax error: Malformed token'. From what I can tell the JSON is correctly formatted with all brackets balanced.

Code is running on 64bit Windows 7.

Anyone got an idea why it thinks this (or how I can convert the stringstream_t to a string and see what it actually reads).

Answer1:

    <li>Could it be that the file is of utf16 encoding? </li> <li>Or check if your json file has BOM (Byte Oder Mark) at the head by opening it with a hex editor.</li> </ul>

    Answer2:

    change the line

    v.parse(s);

    to

    v = json::value::parse(s)

Recommend

  • Changing factors to Integers without changing the order of the data
  • reading a file of key-value pairs in to a std::map
  • Groupby DateTime for specific intervals
  • Are objects in Objective-C ever created on the stack?
  • Force ofstream file flush on Windows
  • In Python, how should one extract the second-last directory name in a path?
  • Any function in numpy/pandas/python to search and replace
  • Random access image (picture) file
  • Performance difference between accessing local and class member variables
  • javafx 8 dialog and concurrency
  • PHP mail() function not delivering mail
  • Android Oreo JobIntentService Keep running in background for Android 7 &below and crashing often
  • Is it possible to specialize on a static lifetime?
  • File random access in J2ME
  • Difference between two forms of Perl filename wildcard command
  • What is RSL (Runtime shared library ) used for in flash?
  • get all processes in parallel
  • Changing a global variable in C
  • How gzip file gets stored in HDFS
  • Error processing multiple files
  • Why does it draw lines in the wrong place?
  • How VBA declared Volatility works
  • How to determine if there are bytes available to be read from boost:asio:serial_port
  • Installed module is empty
  • Converting a WriteableBitmap image ToArray in UWP
  • MVC3 Razor - ListBox pre-select not working
  • Why value captured by reference in lambda is broken? [duplicate]
  • FB SDK and cURL: Unknown SSL protocol error in connection to graph.facebook.com:443
  • TFS: Get latest causes slow project reloading
  • javascript inside java/jsp code
  • Running a C# exe file
  • How to handle AllServersUnavailable Exception
  • Is there a mandatory requirement to switch app.yaml?
  • How to disable jQuery.jplayer autoplay?
  • What are the advantages and disadvantages of reading an entire file into a single String as opposed
  • costura.fody for a dll that references another dll
  • Reading document lines to the user (python)
  • Observable and ngFor in Angular 2
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal