I tried your JSON string here: [C++] gcc 6.3.0 - Wandbox
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
//
// This example shows how to deserialize a JSON document with ArduinoJson.
#include <iostream>
#include "ArduinoJson.h"
int main()
{
// Memory pool for JSON object tree.
//
// Inside the brackets, 200 is the size of the pool in bytes,
// If the JSON object is more complex, you need to increase that value.
// See https://bblanchon.github.io/ArduinoJson/assistant/
StaticJsonBuffer<3000> jsonBuffer;
// StaticJsonBuffer allocates memory on the stack, it can be
// replaced by DynamicJsonBuffer which allocates in the heap.
//
// DynamicJsonBuffer jsonBuffer(200);
// JSON input string.
//
// It's better to use a char[] as shown here.
// If you use a const char* or a String, ArduinoJson will
// have to make a copy of the input in the JsonBuffer.
char json[] =
R"(
{
"Body" : {
"Data" : {
"DAY_ENERGY" : {
"Unit" : "Wh",
"Value" : 60.799999999999997
},
"DeviceStatus" : {
"ErrorCode" : 0,
"LEDColor" : 2,
"LEDState" : 0,
"MgmtTimerRemainingTime" : -1,
"StateToReset" : false,
"StatusCode" : 7
},
"FAC" : {
"Unit" : "Hz",
"Value" : 50.079999999999998
},
"IAC" : {
"Unit" : "A",
"Value" : 0.63
},
"IDC" : {
"Unit" : "A",
"Value" : 0.48999999999999999
},
"PAC" : {
"Unit" : "W",
"Value" : 156
},
"TOTAL_ENERGY" : {
"Unit" : "Wh",
"Value" : 377835.03000000003
},
"UAC" : {
"Unit" : "V",
"Value" : 248.40000000000001
},
"UDC" : {
"Unit" : "V",
"Value" : 348.10000000000002
},
"YEAR_ENERGY" : {
"Unit" : "Wh",
"Value" : 377835
}
}
},
"Head" : {
"RequestArguments" : {
"DataCollection" : "CommonInverterData",
"DeviceClass" : "Inverter",
"DeviceId" : "1",
"Scope" : "Device"
},
"Status" : {
"Code" : 0,
"Reason" : "",
"UserMessage" : ""
},
"Timestamp" : "2018-07-13T07:26:31+10:00"
}
}
)" ;
// Root of the object tree.
//
// It's a reference to the JsonObject, the actual bytes are inside the
// JsonBuffer with all the other nodes of the object tree.
// Memory is freed when jsonBuffer goes out of scope.
JsonObject& root = jsonBuffer.parseObject(json);
// Test if parsing succeeds.
if (!root.success()) {
std::cerr << "parseObject() failed" << std::endl;
return 1;
}
// Fetch values.
//
// Most of the time, you can rely on the implicit casts.
// In other case, you can do root["time"].as<long>();
// const char* sensor = root["sensor"];
// long time = root["time"];
// double latitude = root["data"][0];
// double longitude = root["data"][1];
// Print values.
std::cout << root["Body"]["Data"]["PAC"]["Value"] << std::endl;
// std::cout << time << std::endl;
// std::cout << latitude << std::endl;
// std::cout << longitude << std::endl;
return 0;
}
This is how you de-nest to get your desired value:
root["Body"]["Data"]["PAC"]["Value"]