boostorg / property_tree

Boost.org property_tree module
http://boost.org/libs/property_tree
55 stars 92 forks source link

How to parse nested level of arrays in a json using boosts property tree? #76

Closed Neeraj0019 closed 3 years ago

Neeraj0019 commented 3 years ago

Hi, I want to parse below JSON data and get the "netconf-clients" available. Can someone help me on how to do it.

('{"o-ran-dhcp:dhcp": {"interfaces": [{"interface": "mPlaneVlan295", "dhcpv4": ' '{"client-id": "53148/473966A.101/L1174115125", "dhcp-server-identifier": ''"192.168.100.1", "interface-mtu": 1500, "domain-name": "nokia.com", ' '"domain-name-servers": ["192.168.1.1", "192.168.1.2"], "default-gateways": ''["192.168.100.1"], **"netconf-clients": [{"client": "192.168.100.1", ''"optional-port": 4334}, {"client": "xyz.com", "optional-port": 4334}], '** '"ca-ra-servers": [{"servers": "192.168.100.1", "port-number": 99, ' '"ca-ra-path": "var/opt/z", "subject-name": "radio", "protocol": "HTTPS"}], ' '"segw": [{"gateways": "1.2.3.4"}], "local-ip": "192.168.100.3"}, "dhcpv6": ''{}}], "m-plane-dhcp": {"private-enterprise-num": 53148, "vendor-class-data": ' '"o-ran/No125"}}}')

Br, Neeraj

Neeraj0019 commented 3 years ago

Hi, Below code seems to work.

include <boost/property_tree/ptree.hpp>

include <boost/property_tree/json_parser.hpp>

include

using boost::property_tree::ptree;

int main() {

std::string ss = "{\"o-ran-dhcp:dhcp\": {\"interfaces\": [{\"interface\": \"mPlaneVlan295\", \"dhcpv4\": {\"client-id\": \"174115125\", \"dhcp-server-identifier\": \"192.168.100.1\", \"interface-mtu\": 1500, \"domain-name\": \"xyz.com\", \"domain-name-servers\": [\"192.168.1.1\", \"192.168.1.2\"], \"default-gateways\": [\"192.168.100.1\"], \"netconf-clients\": [{\"client\": \"192.168.100.1\", \"optional-port\": 4334}, {\"client\": \"nokia.com\", \"optional-port\": 4334}], \"ca-ra-servers\": [{\"servers\": \"192.168.100.1\", \"port-number\": 99, \"ca-ra-path\": \"var/opt/z\", \"subject-name\": \"radio\", \"protocol\": \"HTTPS\"}], \"segw\": [{\"gateways\": \"1.2.3.4\"}], \"local-ip\": \"192.168.100.3\"}, \"dhcpv6\": {}}], \"m-plane-dhcp\": {\"private-enterprise-num\": 53148, \"vendor-class-data\": \"o-r115125\"}}}";

ptree pt; std::istringstream is(ss); read_json(is, pt);

boost::property_tree::ptree dhcp = pt.get_child("o-ran-dhcp:dhcp"); boost::property_tree::ptree ifaces = dhcp.get_child("interfaces");

for (auto& e : ifaces.get_child("")) { ptree dhcp = e.second.get_child("dhcpv4"); ptree clients = dhcp.get_child("netconf-clients"); for (auto& e : clients.get_child("")) { std::cout << "client name: " << e.second.get("client") << "\n"; std::cout << "optional port name: " << e.second.get("optional-port") << "\n"; }

}

}

Is this correct way?

Br, Neeraj

madmongo1 commented 3 years ago

Yes this seems correct