使用C+通过Zmq发送JSON对象+;客户端-Python服务器
我试图通过C++客户端和Python服务器使用JoMun../P>交换JSON对象。 server.py使用C+通过Zmq发送JSON对象+;客户端-Python服务器,python,c++,json,zeromq,Python,C++,Json,Zeromq,我试图通过C++客户端和Python服务器使用JoMun../P>交换JSON对象。 server.py import zmq import json context = zmq.Context() socket = context.socket(zmq.REP) socket.bind("tcp://*:5555") while True: json_str = socket.recv_json() data_print = json.loads(json_
import zmq
import json
context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind("tcp://*:5555")
while True:
json_str = socket.recv_json()
data_print = json.loads(json_str)
Type = data_print['Type']
Parameter = data_print['Parameter']
Value = data_print['Value']
print(Type,Parameter,Value)
client.cpp
#include <zmq.hpp>
#include <string>
#include <iostream>
#include <sstream>
#include <json/json.h>
#include <typeinfo>
class multi_usrp_emulation{
public:
void client1(){
std::string strJson="{\"Type\":\"TX\", \
\"Parameter\" : \"Frequency\" ,\
\"Value\" : \"5.17e9\" \
}";
Json::Value root;
Json::Reader reader;
reader.parse(strJson.c_str(),root);
Json::FastWriter fastwriter;
std::string message = fastwriter.write(root);
zmq::context_t context (1);
zmq::socket_t socket (context, ZMQ_REQ);
socket.connect ("tcp://localhost:5555");
zmq::message_t request (message.size());
memcpy (request.data (), (message.c_str()), (message.size()));
socket.send(request);
}
};
int main (void)
{
multi_usrp_emulation caller;
caller.client1();
}
我在C++中使用jsNCPP为JSON。< /P>
如何在C++与Python之间交换JSON消息?< /P> < P>你试图将JSON字符串转换为Python对象两次。以下两行都返回对象,而不是字符串
json_str = socket.recv_json()
data_print = json.loads(json_str)
要么用<代码>套接字.RevvjJONE()/CULL接收数据,然后删除它的代码,或者用<代码>套接字.ReCVE()/Cuffic接收数据,然后将代码中的字符串加载到代码< jSONYSTR < />到Python对象中,使用<代码> JSON.加载(JSONYSTR) .< /P>有一个C++方法用于<代码> Socket。这个页面记录了api的一个稍旧的版本,但是在socket对象上似乎没有任何方法可以直接处理json。
json_str = socket.recv_json()
data_print = json.loads(json_str)