我正在构建一个C++项目,它连接到一个客户拥有的Python代码库并向Jython代码发送JSON对象。由于python代码库是客户所有的,因此我无法修改python代码库以接收json字符串。我正在使用PythonC/API作为接口。
我构建了一个小python文件,使用python的json库将json字符串转换为json对象。我更愿意用Rabijson在C++侧写JSON对象。我能从C++快速JSON构建Python JSON对象并让它在Python的JSON库中读取而不将对象转换为字符串吗?如果可能,哪个函数将是将json文档转换为Python对象的正确函数?
C++:
PyObject* packJsonDict;
PyObject* customerDict;
int PYCPP::getJsonToInterface()
{
std::string json= "{\"pet\": \"dog\", \"count\": 5}";
PyObject* JsonObject = m_pImpl->pack_JsonString(json);
std::string function_string = "read_json";
PyObject* args = PyTuple_Pack(1, JsonObject);
//trying packing just the json string
PyObject* runnableFunction = PyDict_GetItemString(customerDict, (char*)function_string);
PyObject* result = PyObject_CallObject(runnableFunction, args);
}
PyObject* PYCPP::pack_JsonString(std::string json_string)
{
std::string function_string = "convert_jsonString_to_jsonObject";
PyObject* args = PyTuple_Pack(1, PyString_FromString((char*) json_string.c_str()) );
//trying packing just the json string
PyObject* runnableFunction = PyDict_GetItemString(packJsonDict, (char*)function_string);
PyObject* result = PyObject_CallObject(runnableFunction, args);
if(result == nullptr)
{
std::cout << "result was null!\n";
}
return result;
}
Python:
import json
def convert_jsonString_to_jsonObject(jsonString):
print "converting:", jsonString
return json.loads(jsonString)
目前没有回答
相关问题 更多 >
编程相关推荐