如何使用g ++将jsoncpp与C++程序链接?我试过了:
g++ -o program program.cpp -L/path/to/library/files -ljsoncpp, -ljson, -llibjsoncpp
Run Code Online (Sandbox Code Playgroud)
但是g ++一直在说:
/usr/bin/ld: cannot find -lsomething
Run Code Online (Sandbox Code Playgroud) 我严格遵循此文档在我的项目中安装和使用jsoncpp库:jsoncpp README
但是我的编译仍然存在这个问题:
g ++ -W -Wall -Werror -c -o src/ModConnection.o src/ModConnection.cpp src/ModConnection.cpp:15:23:致命错误:json/json.h:没有终止此类文件或目录编译.
这是在我尝试使用时发生的 #include <json/json.h>
这是我的Linux MAKEFILE:
CXX = g++
NAME = bin/server
SRCS = ./src/ModConnection.cpp\
./src/unixNetwork.cpp
OBJS = $(SRCS:.cpp=.o)
CXXFLAGS += -W -Wall -Werror
LDFLAGS = -L ./src/jsoncpp-src-0.5.0/buildscons/linux-gcc4.5.1/src/lib_json/libjson_linux-gcc-4.5.1_libmt.a -I src/jsoncpp-src-0.5.0/include
RM = rm -f
$(NAME) : $(OBJS)
$(CXX) $(LDFLAGS) -o $(NAME) $(OBJS)
all : $(NAME)
clean :
$(RM) $(OBJS)
fclean : clean
$(RM) $(NAME)
re : fclean all
.PHONY : all clean fclean re
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助.
JSON文件如下所示:
{
"strings": [
{
"key_one": "value_one!"
},
{
"key_two": "value_two!"
},
]
}
Run Code Online (Sandbox Code Playgroud)
C++文件如下所示:
Json::Value root;
Json::Reader reader;
bool parsingSuccessful = reader.parse(contents, root);
const Json::Value strings = root["strings"];
std::vector<std::string> list = strings.getMemberNames();
Run Code Online (Sandbox Code Playgroud)
"strings.getMemberNames()"引起的错误是:
Assertion failed: (type_ == nullValue || type_ == objectValue), function getMemberNames, file /projects/.../jsoncpp.cpp,
Run Code Online (Sandbox Code Playgroud)
strings是一个arrayValue,我通过它确认了它ValueType = 6.
我想jsoncpp用来编写C++代码来解析JSON文件.让我解释一下我做了什么.我创建了一个CMakeLists.txt和FindJsoncpp.cmake一个简单的c ++文件进行测试jsoncpp.当我在没有使用cmake的情况下编译C++源代码时,-I/usr/include/jsoncpp/ -ljsoncpp它运行正常.但是当我尝试使用cmake构建它时,它找不到json.h我在c ++源代码中包含的头文件.
这是我的CMakeLists.txt:
cmake_minimum_required (VERSION 2.6)
project (Parser)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")
include(LibFindMacros)
message("----------- trying to find Jsoncpp-------------")
find_package(Jsoncpp)
if(Jsoncpp_FOUND)
message("INFO: we found LibJsoncpp on your pc.")
message(Jsoncpp_FOUND = ${Jsoncpp_FOUND})
message(Jsoncpp_INCLUDE_DIR = ${Jsoncpp_INCLUDE_DIR})
message(Jsoncpp_LIBRARY = ${Jsoncpp_LIBRARY})
else(Jsoncpp_FOUND)
message("WARNING: we couldn't find LibJsoncpp on your pc. DLC is disabled.")
endif(Jsoncpp_FOUND)
#set(LIBS ${Jsoncpp_LIBRARY})
# Set the include dir variables and the libraries and let libfind_process do the …Run Code Online (Sandbox Code Playgroud) 我有一个Config文件,其中包含以下内容:
{
"ip": "127.0.0.1",
"heartbeat": "1",
"ssl": "False",
"log_severity": "debug",
"port":"9999"
}
Run Code Online (Sandbox Code Playgroud)
我用JsonCpp来读取上面配置文件的内容.读取Config File的内容工作正常,但在Config File中写入内容失败.我有以下代码:
#include <json/json.h>
#include <json/writer.h>
#include <iostream>
#include <fstream>
int main()
{
Json::Value root; // will contains the root value after parsing.
Json::Reader reader;
Json::StyledStreamWriter writer;
std::ifstream test("C://SomeFolder//lpa.config");
bool parsingSuccessful = reader.parse( test, root );
if ( !parsingSuccessful )
{
// report to the user the failure and their locations in the document.
std::cout << "Failed to parse configuration: "<< reader.getFormattedErrorMessages();
}
std::cout << root["heartbeat"] << …Run Code Online (Sandbox Code Playgroud) 我正在使用Jsoncpp使用Json :: FastWriter将Json :: value写入字符串.
string s;
s.append("me?json=");
val["firstname"] = firstname;
val["lastname"] = lastname;
val["x"] = me->myPos.X;
val["y"] = me->myPos.Y;
val["z"] = me->myPos.Z;
val["lookx"] = me->myOri.X;
val["looky"] = me->myOri.Y;
val["lookz"] = me->myOri.Z;
url.append(writer.write(val));
Run Code Online (Sandbox Code Playgroud)
问题是他们没有在字符串中出现在我加入他们将JSON ::值VAL的顺序,他们似乎是按字母顺序排序取决于第一个字母中的每个元素("名字,姓氏,lookx,听我说,lookz,x"等).你怎么防止这种情况?我希望它按照我将它添加到Json ::值的顺序添加,而不是排序.
如果这不可能,那么如何改变源代码来实现呢?
谢谢
我正在使用JsonCpp解析json数据.我真的不需要理解数据,我只需要打印出一些属性及其值.它不知何故很难做到.首先,我需要知道值是什么类型,然后获取值,然后再将其转换为字符串!有一个风格的作家,但我不想使用它,因为它最后添加了一些CRLF.
我这样做
CJsonHelper::getUInt(Json::Value &root, std::string key){
return root.get(key, 0-1).isInt() ? root.get(key, 0-1).asUInt() : 0-1;
}
Run Code Online (Sandbox Code Playgroud)
我可以只编写一个函数来获取所有属性,而这个函数并不真正关心类型等吗?
我有一个示例文件"sample.json",其中包含3个json对象
{ "A": "something1", "B": "something2", "C": "something3", "d": "something4"} { "A": "something5", "B": "something6", "C": "something7", "d": "something8"} { "A": "something9", "B": "something10", "C": "something11", "d": "something12"}
(上面的文件中没有换行符)
我想用jsoncpp读取所有三个json对象.
我能够读取第一个对象但不能读取它.
这是我的代码的相关部分
Json::Value root; // will contains the root value after parsing.
Json::Reader reader;
std::ifstream test("sample.json", std::ifstream::binary);
bool parsingSuccessful = reader.parse(test, root, false);
int N = 3;
if (parsingSuccessful)
{
for (size_t i = 0; i < N; i++)
{
std::string A= root.get("A", "ASCII").asString();
std::string B= root.get("B", "ASCII").asString();
std::string C= …Run Code Online (Sandbox Code Playgroud) 我被jsoncpp困住了。我想创建一个像这样的数组:
"Cords": [{"x": 10, "y": 20}, {"x": 70, "y": 40}, {"x": 15, "y": 65}]
Run Code Online (Sandbox Code Playgroud)
我设法用 jsoncpp 做常规的事情(见下文),但我陷入了制作 JSON 数组的情况。
Json::Value event;
event["name"] = "Joe";
event["Direction"]["left"]["x"] = "1338";
event["Direction"]["right"]["x"] = "1337";
Run Code Online (Sandbox Code Playgroud)
编辑:
我想在事件中打印所有内容。
我不想单独打印电线。
我有一个要序列化的jsoncpp值。最直接的方法是这样的:
Json::Value val; // population is left as an exercise for the reader
std::string str = Json::FastWriter().write(val);
Run Code Online (Sandbox Code Playgroud)
问题是它FastWriter已被弃用,我不能容忍编译器警告。根据不太直观的文档,我应该StreamWriterBuilder改用:
Json::StreamWriterBuilder builder;
builder["commentStyle"] = "None";
builder["indentation"] = "";
std::unique_ptr<Json::StreamWriter> writer( builder.newStreamWriter() );
std::ostringstream os;
writer->write(val, &os);
std::string str = os.str();
Run Code Online (Sandbox Code Playgroud)
这肯定不能“更好”吗?我假设错误在于我,并且有一种简单的方法来执行最小序列化(没有多余的空格)。
这显示了一个稍微更紧凑的形式(尽管它似乎只是将上面的内容包装在一个函数调用中)。
Json::StreamWriterBuilder builder;
builder["indentation"] = ""; // assume default for comments is None
std::string str = Json::writeString(builder, val);
Run Code Online (Sandbox Code Playgroud)
现在是正确的方法吗?