我现在只使用jsonCPP lib几个月了.我试图添加和删除数组中的对象.我已经在不同的平台上使用了许多不同的JSON库,但我发现使用JsonCPP非常困难.
这是Json:
{ "type": "Disc",
"media": "DVD",
"adapter": "DVDCodecs",
"transportControls" : [
{"Action":"Left", "ActionCode" : "1a"},
{"Action":"Right", "ActionCode" : "2a"},
{"Action":"Up", "ActionCode" : "1b"},
{"Action":"Down", "ActionCode" : "4c"},
{"Action":"Center", "ActionCode" : "5e"},
{"Action":"OK", "ActionCode" : "5a"},
{"Action":"SubTitles", "ActionCode" : "3b"},
{"Action":"SubTitlesLang", "ActionCode" : "7d"},
{"Action":"Audio", "ActionCode" : "7a"},
{"Action":"Angle", "ActionCode" : "6a"},
{"Action":"Next", "ActionCode" : "6c"},
{"Action":"Previous", "ActionCode" : "8b"},
{"Action":"DVDMenu", "ActionCode" : "8c"},
{"Action":"Search", "ActionCode" : "8d"},
{"Action":"Region", "ActionCode" : "3a"},
{"Action":"Display", "ActionCode" : "2e"},
{"Action":"RootMenu", "ActionCode" : …Run Code Online (Sandbox Code Playgroud) 我需要在将新文件写入磁盘时更新索引(采用JSON格式),并且由于文件是分类的,我使用的是具有这种结构的对象:
{ "type_1" : [ "file_1", "file_2" ], "type_2" : [ "file_3", "file_4" ] }
Run Code Online (Sandbox Code Playgroud)
我认为这对jsoncpp来说是一项轻松的任务,但我可能会遗漏一些东西.
我的代码(简化)在这里:
std::ifstream idx_i(_index.c_str());
Json::Value root;
Json::Value elements;
if (!idx_i.good()) { // probably doesn't exist
root[type] = elements = Json::arrayValue;
} else {
Json::Reader reader;
reader.parse(idx_i, root, false);
elements = root[type];
if (elements.isNull()) {
root[type] = elements = Json::arrayValue;
}
idx_i.close();
}
elements.append(name.c_str()); // <--- HERE LIES THE PROBLEM!!!
std::ofstream idx_o(_index.c_str());
if (idx_o.good()) {
idx_o << root;
idx_o.close();
} else {
Log_ERR << "I/O error, …Run Code Online (Sandbox Code Playgroud) 我有这个JSON对象:
{"books":[
{
"author" : "Petr",
"book_name" : "Test1",
"pages" : 200,
"year" : 2002
},
{
"author" : "Petr",
"book_name" : "Test2",
"pages" : 0,
"year" : 0
},
{
"author" : "STO",
"book_name" : "Rocks",
"pages" : 100,
"year" : 2002
}
]
}
Run Code Online (Sandbox Code Playgroud)
例如,我需要找一本author键等于的书Petr.我怎样才能做到这一点?现在我有这段代码:
Json::Value findBook(){
Json::Value root = getRoot();
cout<<root["books"].toStyledString()<<endl; //Prints JSON array of books mentioned above
string searchKey;
cout<<"Enter search key: ";
cin>>searchKey;
string searchValue;
cout<<"Enter search value: ";
cin>>searchValue;
Json::Value …Run Code Online (Sandbox Code Playgroud) 我正在使用jsoncpp从JSON文件中读取.当我写回文件时,我的浮点值略有偏差.为了测试,我决定将文件解析为Json :: Value,然后将该值写回文件.我希望它看起来一样,但浮动值不同.
例:
"Parameters":
{
"MinXValue": 0.1,
"MaxXValue": 0.15,
"MinYValue": 0.25,
"MaxYValue": 1.1,
"MinObjectSizeValue": 1
}
Run Code Online (Sandbox Code Playgroud)
写道:
"Parameters":
{
"MinXValue": 0.10000000000000001,
"MaxXValue": 0.14999999999999999,
"MinYValue": 0.25,
"MaxYValue": 1.1000000238418579,
"MinObjectSizeValue": 1
}
Run Code Online (Sandbox Code Playgroud)
您可能会注意到0.25没有改变,即使所有其他浮动都没有改变.知道这里发生了什么吗?
我是 cpp 的新手,但根据项目要求,我需要使用 rest api 并解析响应。能够调用 api 并捕获响应,但无法使用 JSONCPP 库解析它。
这些是我解析json所遵循的步骤:
它总是给予
fatal error: json/json.h: No such file or directory
#include <json/json.h>
^
compilation terminated.
Run Code Online (Sandbox Code Playgroud)
自 2 天以来没有找到任何解决方案。这个也试过了。谁能告诉我我哪里出错了...
我试图使用jsoncpp来解析一组json.json是使用来自django对象的simplejson从网页生成的.我使用libcurl从特定的URL获取它.当我在根上使用toStyledString()函数时,我将其打印出来.
[
{
"fields" : {
"desc" : "Carol King test",
"format" : "1",
"genre" : "Pop",
"mount" : "CarolKing",
"name" : "Carol King",
"protocol" : "0",
"songs" : [ 27, 28, 29, 30, 31, 32, 33, 34 ],
"url" : "http://192.168.0.5:8000/CarolKing"
},
"model" : "music.playlist",
"pk" : 2
}
]
Run Code Online (Sandbox Code Playgroud)
所以看起来我正在获取数据并将其放在Json :: Value类中.
问题是我无法从json结构中获取特定节点.这是我正在使用的代码.
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <sstream>
#include <curl/curl.h>
#include <string>
#include "Parameter.h"
#include "lib_json/json.h"
using namespace std;
static size_t write_data(void *ptr, size_t size, …Run Code Online (Sandbox Code Playgroud) 我不确定这是否是jsoncpp的特定内容或者是如何使C++库表现更好的一般范例.基本上我得到这个痕迹:
imagegeneratormanager.tsk: src/lib_json/json_value.cpp:1176: const Json::Value& Json::Value::operator[](const char*) const: Assertion `type_ == nullValue || type_ == objectValue' failed.
Run Code Online (Sandbox Code Playgroud)
当输入不好时就会发生这种情况.当输入 - 来自我的另一个应用程序通过memcached - 碰巧是坏的,我想处理这个错误.你知道,优雅.也许类似于"错误:项目15006的输入是坏的"进入日志.不会崩溃我的整个JSON字符串处理任务.
这只是一个写得不好的库还是可以更巧妙地配置它?
编辑:这是一些调用代码:
Json::Value root;
Json::Reader reader;
succeeded = reader.parse(jsonString, root);
if(!succeeded) {
throw std::runtime_error(std::string("Failed to parse JSON for key ") + emailInfoKey.str());
}
std::string userEmail = root.get("userId", "").asString();
std::string bodyFilePath = root.get("bodyFilePath", "").asString();
std::string msgId = root.get("msgId", "").asString();
Run Code Online (Sandbox Code Playgroud) 我正在使用jsoncpp,有没有办法在Json :: Value中放置一个地图?
我知道我可以逐个提取密钥并将它们放在Json :: Value中.但是有更优雅的方式吗?例如在C#计数器部分NewtonSoft json api
我会做
String json = JsonConvert.SerializeObject(cmdDict);
Run Code Online (Sandbox Code Playgroud) 考虑以下示例,我的来源是
Json::Value root;
root["id"]=0;
Json::Value text;
text["first"]="i";
text["second"]="love";
text["third"]="you";
root["text"]=text;
root["type"]="test";
root["begin"]=1;
root["end"]=1;
Json::StyledWriter writer;
string strJson=writer.write(root);
cout<<"JSON WriteTest" << endl << strJson <<endl;
Run Code Online (Sandbox Code Playgroud)
我想我应该按照行的顺序编写 json 字段。相反,结果是:
JSON WriteTest
{
"begin" : 1,
"end" : 1,
"id" : 0,
"text" : {
"first" : "i",
"second" : "love",
"third" : "you"
},
"type" : "test"
}
Run Code Online (Sandbox Code Playgroud)
我想要的json格式是
JSON WriteTest
{
"id" : 0,
"text" : {
"first" : "i",
"second" : "love",
"third" : "you"
},
"type" : "test"
"begin" …Run Code Online (Sandbox Code Playgroud) 使用 jsoncpp,我正在创建一个 JSON 对象。
Json::Value root;
Json::Value zone1;
Json::Value coord;
Json::Value gridOrigin;
JSON_PEOPLE(){
zone1["zoneID"] = "shop1";
zone1["stamp"] = "##########";
zone1["gridSizeX"]=50;
zone1["gridSizeY"]=50;
zone1["gridScale"]=0.5;
zone1["gridOrigin"].append(28.5);
zone1["gridOrigin"].append(20.6);
}
std::string get_time_stamp()
{
time_t rawtime;
std::time(&rawtime);
struct tm *tinfo = std::localtime(&rawtime);
char buf[50];
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", tinfo);
return std::string(buf);
}
std::string updateZone (std::vector < std::pair <int,int> > &world_coords){
zone1["stamp"]=get_time_stamp();
coord.clear();
zone1["detected_people"].clear();
for(int i=0; i < world_coords.size(); i++){
Json::Value person;
person.append(world_coords[i].first);person.append(world_coords[i].second);
coord["coordinates"].append(person);
}
zone1["detected_people"] = coord;
root["zone1"]=zone1;
Json::StreamWriterBuilder builder;
const std::string json_file = Json::writeString(builder, root); …Run Code Online (Sandbox Code Playgroud)