exception while parsing a string

Jan 23, 2014 at 6:12 AM
I am trying to parse a string but always end up with a `not a string` exception. 

utility::string_t data = dataTextBox->Text->Data();
                    try{
                        OutputDebugString(data.c_str());//output is::{"name":"new fold"}
                        web::json::value jsonData = web::json::value::parse(data);
                        OutputDebugString(jsonData.as_string().c_str());
                    }
                    catch (std::exception& exc){
                        OutputDebugString(utility::conversions::to_string_t( exc.what()).c_str());
                    }
Jan 23, 2014 at 6:20 AM
Edited Jan 23, 2014 at 6:20 AM
my bad..the exception was being thrown by the second OutputDebugString.
Coordinator
Jan 23, 2014 at 5:46 PM
Hi,

Yes what you are seeing is the fact that json::value::as_string() only works if the actual underlying json value is indeed a string. All the as_* methods act as a bridge to the C++ type system. Think about that method as asking for the json value as a C++ string as opposed to serialization. For example json::value::as_integer only works if the underlying json value is a integer number value. The json::value::to_string() method really is serialization, it looks like that is what you mean to use here. json::value::to_string() synonymous as using json::value::serialize. You can test to see each json value is by using the is_array(), is_number(), is_sting(), etc... methods, or with the json::value::type() method.

Hope that clarifies,
Steve