C++ boost parse dynamically generated json string (not a file)

前端 未结 1 1446
野趣味
野趣味 2020-12-29 07:33

I am trying to make a minimal example of reading a json string that is passed as a command line arg with boost. I am very new to C++ and to boost.

My code is:

1条回答
  •  生来不讨喜
    2020-12-29 07:54

    What you can do is read the characters into a string stream, and then pass that to read_json.

    #include 
    #include 
    
    #include 
    
    int main (int argc, char ** argv)
    {
      std::stringstream ss;
      ss << argv[1];
    
      boost::property_tree::ptree pt;
      boost::property_tree::read_json(ss, pt);
      std::cout << pt.get("foo") << std::endl;
    }
    

    outputs

    bar
    

    0 讨论(0)
提交回复
热议问题