【问题标题】:Why can't assign a string variable to json::value object?为什么不能将字符串变量分配给 json::value 对象?
【发布时间】:2017-10-21 08:23:46
【问题描述】:

这种初始化是可能的,可以使json::value对象的值为“hello1”如下。

json::value v1 = json::value::string(U("hello1")); // ok

但是这个初始化不起作用。这是什么原因?如何使用字符串或 char* 等变量的值创建 JSON 对象。

string str1 = "Hello2";
    json::value v2 = json::value::string(str1);   //Error (1)
    json::value v3 = json::value::string(U(str1)); //Error (2)

错误 1

Severity    Code    Description Project File    Line    Suppression State
Error   C2248   'web::json::value::string': cannot access private member declared in class 'web::json::value'   StolenDetailsService    c:\users\nuwanst\source\repos\stolendetailsservice\stolendetailsservice\dbhandler.cpp   62  

错误 2

Severity    Code    Description Project File    Line    Suppression State
Error   C2065   'Lstr1': undeclared identifier  StolenDetailsService    c:\users\nuwanst\source\repos\stolendetailsservice\stolendetailsservice\dbhandler.cpp   62  

【问题讨论】:

  • 我认为你已经交换了错误:U() 看起来像一个将L 粘贴到其参数上的宏,它仅适用于字符串文字,但只会构成非现有的Lstr1 变量在这里。
  • 是的,谢谢我修好了。请告诉我你是否知道还有其他方法可以用字符串变量构造一个 json 对象

标签: c++ json casablanca


【解决方案1】:

我使用了流。它对我有用。

   string st1="Hello";
    utility::stringstream_t ss1;
    ss1 << str1;
    json::value Jobj = json::value::parse(ss1);

【讨论】:

    猜你喜欢
    • 2021-09-09
    • 2020-10-15
    • 2011-07-02
    • 2012-06-17
    • 2021-08-03
    • 1970-01-01
    • 1970-01-01
    • 2019-08-19
    • 1970-01-01
    相关资源
    最近更新 更多