2017-08-25 213 views
1

我正在使用https://github.com/nlohmann/json,它工作正常。但是我發現困難,創建以下JSON outout使用nlohmann json在C++中創建嵌套的json對象

{ 
    "Id": 1, 
    "Child": [ 
     { 
      "Id": 2 
     }, 
     { 
      "Id": 3, 
      "Child": [ 
       { 
        "Id" : 5 
       }, 
       { 
        "Id" : 6 
       } 
      ] 
     }, 
     { 
      "Id": 4 
     } 
    ] 
} 

每個節點都必須有一個id和陣列(「孩子」元素)。任何孩子都可以遞歸地繼續擁有Id或Child。上面的json只是一個例子。我想要的是使用nlohmann json在父子節點之間創建一個鏈。

數字1,2,3 ......隨機挑選。我們現在不關心這些價值。

任何想法如何創建它?

代碼到目前爲止

#include <iostream> 
#include <string> 
#include <vector> 
#include "json.hpp" 

using json = nlohmann::json; 


struct json_node_t { 

    int id; 
    std::vector<json_node_t> child; 
}; 


int main(int argc, char** argv) { 

    json j; 

    for(int i = 0; i < 3; i++) { 

     json_node_t n; 

     n.id = i; 
     j["id"] = i; 

     if (i < 2) { 

      j["child"].push_back(n); 


     } 


    } 


    return 0; 

} 
+0

我們怎麼來推斷,5 6應該是3的孩子,而不是2,比如說2?這篇文章有許多與上一個問題相同的問題(https://stackoverflow.com/questions/45882363/c-create-json-output-from-a-tree-like-input)。 – AndyG

+0

我並不在乎id元素的值。我只想找出如何使用nlohmann json – cateof

回答

1

爲了序列化自己的類型,您需要實現該類型to_json功能。

#include <iostream> 
#include <string> 
#include <vector> 
#include "json.hpp" 

using namespace std; 
using json = nlohmann::json; 

struct json_node_t { 
    int id; 
    std::vector<json_node_t> child; 
}; 

void to_json(json& j, const json_node_t& node) { 
    j = {{"ID", node.id}}; 
    if (!node.child.empty()) 
     j.push_back({"children", node.child}); 
} 

int main() { 
    json_node_t node = {1, {{2, {}}, {3, {{5, {}}, {6, {}}}}, {4, {}}}}; 
    json j = node; 

    cout << j.dump(2) << endl; 
    return 0; 
} 

輸出:

{ 
    "ID": 1, 
    "children": [ 
    { 
     "ID": 2 
    }, 
    { 
     "ID": 3, 
     "children": [ 
     { 
      "ID": 5 
     }, 
     { 
      "ID": 6 
     } 
     ] 
    }, 
    { 
     "ID": 4 
    } 
    ] 
} 

一對夫婦的更多的方式來初始化json_node_t(全部產生相同的樹和相同的輸出):

struct json_node_t { 
    int id; 
    std::vector<json_node_t> child; 
    json_node_t(int node_id, initializer_list<json_node_t> node_children = initializer_list<json_node_t>()); 
    json_node_t& add(const json_node_t& node); 
    json_node_t& add(const initializer_list<json_node_t>& nodes); 
}; 

json_node_t::json_node_t(int node_id, initializer_list<json_node_t> node_children) : id(node_id), child(node_children) { 
} 

json_node_t& json_node_t::add(const json_node_t& node) { 
    child.push_back(node); 
    return child.back(); 
} 

json_node_t& json_node_t::add(const initializer_list<json_node_t>& nodes) { 
    child.insert(child.end(), nodes); 
    return child.back(); 
} 

int main() { 
    json_node_t node_a = {1, {{2, {}}, {3, {{5, {}}, {6, {}}}}, {4, {}}}}; 

    json_node_t node_b = {1, {2, {3, {5, 6}}, 4}}; 

    json_node_t node_c(1); 
    node_c.add(2); 
    node_c.add(3).add({5, 6}); 
    node_c.add(4); 

    cout << json(node_a).dump(2) << endl << endl; 
    cout << json(node_b).dump(2) << endl << endl; 
    cout << json(node_c).dump(2) << endl; 
    return 0; 
} 
+0

謝謝創建數據的遞歸表示。看起來可以使用to_json方法。但是我發現很難創建節點。此初始化工作正常{1,{{2,{}},{3,{{5,{}},{6,{}}}},{4,{}}}};但有沒有更簡單的方法來添加節點,即添加孩子在家長? – cateof

+1

@cateof既然'json_node_t'是你的類,你可以添加任何適合預期用法的便利方法。我在答案中增加了幾個例子。 – Anton