标签: jsoncpp

使用jsoncpp从JSON中获取节点的问题

我试图使用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)

c++ jsoncpp

6
推荐指数
1
解决办法
1万
查看次数

按顺序写成jsoncpp(c++)

考虑以下示例,我的来源是

    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)

c++ json jsoncpp

6
推荐指数
1
解决办法
8955
查看次数

Json::Value::resolveReference(key, end):需要 objectValue

使用 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)

json jsoncpp

6
推荐指数
0
解决办法
3366
查看次数

如何读取包含多个根元素的JSON文件?

如果我有一个文件的内容如下:

{"one": 1}
{"two": 2}
Run Code Online (Sandbox Code Playgroud)

我可以简单地将每个单独的行解析为一个单独的JSON对象(使用JsonCpp).但是如果文件的结构不那么方便怎么办呢:

{
   "one":1
}

{
   "two":2
}
Run Code Online (Sandbox Code Playgroud)

c++ jsoncpp

5
推荐指数
2
解决办法
1万
查看次数

Jsoncpp - 当 Json::reader 超出范围时,非常简单的测试会崩溃

我已经下载并安装了 jsoncpp 库。然后我尝试在我自己的应用程序中使用该库:

#include <json/json.h>

void parseJson() {
   Json::Reader reader;
} 

int main(int argc, char ** argv) {
   parseJson();
   exit(0);
}
Run Code Online (Sandbox Code Playgroud)

该程序编译和链接正常,但在运行时因 SIGSEGV 而崩溃。gdb 回溯如下所示:

(gdb) bt
#0  0x0000003a560b7672 in __gnu_cxx::__exchange_and_add () from /usr/lib64/libstdc++.so.6
#1  0x00000000004031e9 in std::string::_Rep::_M_dispose (this=0xffffffffffffffe9, __a=@0x7fffbfe60e57)
at /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/basic_string.h:232
#2  0x0000000000403236 in ~basic_string (this=0x7fffbfe60fb0)
at /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/basic_string.h:478
#3  0x00000000004038d4 in ~Reader (this=0x7fffbfe60eb0) at /private/joaho/Parser/opm-parser/external/json/json-cpp/include/json/reader.h:23
#4  0x0000000000402990 in parseJson () at /private/joaho/Parser/opm-parser/opm/parser/eclipse/ExternalTests/ExternalTests.cpp:51
#5  0x00000000004029ab in main (argc=1, argv=0x7fffbfe610c8)
at /home/user/Parser/opm-parser/opm/parser/eclipse/ExternalTests/ExternalTests.cpp:56
Run Code Online (Sandbox Code Playgroud)

即对我来说,它似乎在析构函数中崩溃。据我所知 Json::Reader 没有它自己的析构函数,所以这必须是一个默认的析构函数。如您所见,我正在运行一个相当旧版本的 g++ - 这可能是问题所在吗?

c++ jsoncpp

5
推荐指数
1
解决办法
2307
查看次数

JSONCPP 到 Visual Studio

我在将 JSONCPP 库导入 Visual Studio 时遇到了一些问题。

我已经下载了库我只是不确定如何将它导入我的项目并在我的 C++ 代码中使用它。

c++ jsoncpp visual-studio-2013

5
推荐指数
1
解决办法
8890
查看次数

jsoncpp operator= 中未定义的引用

如何解决以下链接器错误 wrt jsoncpp operator=.

我创建了一个Json::Value这样的对象

Json::Value pt;
pt["type"] = 5;
pt["uuid"] = "f8c74622-d45e-4cfa-fe00-5e7042431c72";
pt["start frame"] = 10;
pt["duration"] = 20;
pt["payload"] = "aedddefffsadf";
Run Code Online (Sandbox Code Playgroud)

当我尝试链接 Ubuntu 14.04 附带的默认 libjsoncpp-dev 时,会出现以下链接器错误

对`Json::Value::operator=(Json::Value)'的未定义引用

额外信息:

当编译并链接到来自 GitHub 的最新 jsoncpp 时,我没有收到此链接器错误。

/usr/include/jsoncpp/json 中的 value.h(由 ubuntu apt 安装)有

Value &operator=( const Value &other );
Run Code Online (Sandbox Code Playgroud)

和最新的 jsoncpp value.h 有

Value &operator=(Value other);
Run Code Online (Sandbox Code Playgroud)

c++ jsoncpp c++11 ubuntu-14.04

5
推荐指数
1
解决办法
4124
查看次数

jsoncpp查找成员函数

我在Win32平台上使用jsoncpp 库的v1.6.2。一切都很好,我能够解析 json 并使用etc成功访问。Json:Valuesv.["firstname"]

Json:Value问:如何在 json 结构中按名称查找?在我发现的文档中:

Value const * Json::Value::find ( char const *key,char const *end ) const
Run Code Online (Sandbox Code Playgroud)

但它没有具体说明'end'是什么。我已经尝试过"",但它没有返回任何我可以使用的东西。

我是否应该尝试以这种方式找到东西?

我发现文档非常稀疏,因此一些 jsoncpp 程序示例会非常有帮助。

谢谢

c++ json jsoncpp

5
推荐指数
1
解决办法
1575
查看次数

使用JsonCpp编写JSON时如何忽略空对象

根据Google JSON样式指南,建议删除空值或空值.

使用JsonCpp时,如何从对象结构或写入流中删除空值或空值?

我想要以下代码:

#include <json/json.h>
#include <json/writer.h>

Json::Value json;
json["id"] = 4;
// The "name" property is an empty array.
json["name"] = Json::Value(Json::arrayValue);
Json::FastWriter fw;
std::cout << fw.write(json) << std::endl;
Run Code Online (Sandbox Code Playgroud)

生产:

{
    "id": 4,
}
Run Code Online (Sandbox Code Playgroud)

c++ json jsoncpp

5
推荐指数
1
解决办法
1084
查看次数

如何将 jsoncpp 与 cmake 链接

我找不到将 jsoncpp 与我的可执行文件链接的方法。我尝试了很多事情但没有成功:

find_package(PkgConfig REQUIRED)
pkg_check_modules(JSONCPP jsoncpp)
link_libraries(${JSONCPP_LIBRARIES})
add_executable(myprogram myprogram.cpp)
target_link_libraries(myprogram ${JSONCPP_LIBRARIES})
Run Code Online (Sandbox Code Playgroud)

我想使用ubuntu自带的jsoncpp库。有人设法做到这一点吗?

find_package(PkgConfig REQUIRED)
pkg_check_modules(JSONCPP jsoncpp)
link_libraries(${JSONCPP_LIBRARIES})
add_executable(myprogram myprogram.cpp)
target_link_libraries(myprogram ${JSONCPP_LIBRARIES})
Run Code Online (Sandbox Code Playgroud)

CMakeLists.txt

cmake_minimum_required(VERSION 3.8.0 FATAL_ERROR)

project(jsoncpp_example)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -stdlib=libc++ -Wall -Werror")
set(CMAKE_EXE_LINKER_FLAGS "-lstdc++ -lpthread")

add_executable(parse main.cpp)
target_include_directories(parse PRIVATE include)
target_link_libraries(parse jsoncpp)
Run Code Online (Sandbox Code Playgroud)

解析器.h

$ ls /usr/lib/x86_64-linux-gnu/libjsoncpp.*

/usr/lib/x86_64-linux-gnu/libjsoncpp.a   /usr/lib/x86_64-linux-gnu/libjsoncpp.so.1
/usr/lib/x86_64-linux-gnu/libjsoncpp.so  /usr/lib/x86_64-linux-gnu/libjsoncpp.so.1.7.4
Run Code Online (Sandbox Code Playgroud)

主程序

cmake_minimum_required(VERSION 3.8.0 FATAL_ERROR)

project(jsoncpp_example)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -stdlib=libc++ -Wall -Werror")
set(CMAKE_EXE_LINKER_FLAGS "-lstdc++ -lpthread")

add_executable(parse main.cpp)
target_include_directories(parse PRIVATE include)
target_link_libraries(parse jsoncpp)
Run Code Online (Sandbox Code Playgroud)

Ubuntu 18.04,使用 clan-8.0 编译 …

c++ linux json cmake jsoncpp

5
推荐指数
1
解决办法
1万
查看次数

标签 统计

jsoncpp ×10

c++ ×9

json ×5

c++11 ×1

cmake ×1

linux ×1

ubuntu-14.04 ×1

visual-studio-2013 ×1