使用 JsonCPP 创建 Json 消息

Create Json Message using JsonCpp

本文关键字:消息 Json 创建 JsonCPP 使用      更新时间:2023-10-16

我正在尝试使用 Jsoncpp 创建 Json 消息。我已经做了如下:

#include <string>
#include <iostream>
#include <sstream>    
#include <json/json.h>
int main()
{
  std::string Value = "5.17e9";
  std::string Type = "TX";
  std::string Parameter = "Frequency";
  Json::Value root; 
  root.append("Type");
  root.append("Parameter");
  root.append("Value");
  root["Type"] = Type;
  root["Parameter"] = Parameter;
  root["Value"] = Value; 
  Json::FastWriter fastwriter;
  std::string message = fastwriter.write(root);
  std::cout<<message<<std::endl;
  return 0;
 }

使用以下命令行编译此代码:

g++ -o clients clients.cpp -ljsoncpp -lzmq

发生此类错误:

clients.cpp:(.text._ZN20multi_usrp_emulation7client1Ev[_ZN20multi_usrp_emulation7client1Ev    ]+0x1d9): undefined reference to `Json::Value::operator=(Json::Value)'
clients.cpp:(.text._ZN20multi_usrp_emulation7client1Ev[_ZN20multi_usrp_emulation7client1Ev    ]+0x224): undefined reference to `Json::Value::operator=(Json::Value)'
clients.cpp:(.text._ZN20multi_usrp_emulation7client1Ev[_ZN20multi_usrp_emulation7client1Ev    ]+0x26c): undefined reference to `Json::Value::operator=(Json::Value)'
collect2: error: ld returned 1 exit status

我的代码有什么问题?

我不确定链接错误,但是代码存在问题,在编译器中可能会以不同的方式处理。这对我来说是一个运行时错误。

Json::Value root; 
root.append("Type"); // makes root into arrayValue
root["Type"] = Type; // accesses root as an objectValue
// triggers assert in Json::Value::resolveReference

这就是我的做法:

Json::Value root; 
root["Type"] = Type;