0

So, I access an API an receive data in JSON format as shown below. I'm looking to draw a chart with the dates on the "x" axis and the Data on the "y" axis. I don´t know what dates I will be getting, and they will be plenty. I want to save that data in an array/vector of objects that contain a structure containing the date and value at that date. Because this isn't a key-value relation, I do not know how to parse it, do you?

Structure

typedef struct
{
    float Data;
    string date;
}Data_t;

JSON received

 "Information: Data": {
        "2021-06-01 16:00:01": {
            "Data": "139.5578"
        },
        "2021-05-28": {
            "Data": "137.7645"
        },
        "2021-05-21": {
            "Data": "135.8931"
        },
        "2021-05-14": {
            "Data": "133.6110" 
        }
...
...
...
  • Out of curiosity, why are you using C++ for this? And more importantly, **what C++ JSON library are you using?** – Dai Jun 02 '21 at 02:24
  • If you're writing C++ then *stop writing C* - you don't need to compromise your code's readability by (for example) using `typedef struct` ([which is unnecessary in C++ as structs/tags/etc don't exist in their own namespace unlike in C](https://stackoverflow.com/questions/612328/difference-between-struct-and-typedef-struct-in-c)). **C++ is not C**. – Dai Jun 02 '21 at 02:25
  • what about `std::map data;` where the key is the string and the string is also in the structure? Or you could use a map iterator which results in a std::pair of the key and the data - then you wouldn't need the structure at all. – Jerry Jeremiah Jun 02 '21 at 02:26
  • 1
    Your `Data` values are actually JSON `String` values, not `Number` values - which suggests that `Data` can contain non-numeric values. Also, `float` in C++ is typically a 32-bit IEEE-754 number, whereas `Number` in JSON is a 64-bit IEEE-754 number, so you'd want to use `double`, not `float`. – Dai Jun 02 '21 at 02:26
  • If you don't absolutely need a struct then what about something like this: https://onlinegdb.com/Strr9sPH3 – Jerry Jeremiah Jun 02 '21 at 02:56
  • Im new to stack overflow, i dont know how to answer comments. So ill anwer the 5 comments here. 1. im using nolhamnn json (dont kll me on the spelling). And im using c++ because its what i know 2. ok, but the structure isnt really relevant to my problem :) 4. i know, i was gonna create a function for that, but got stuck with this first 5. k, structures bad, got it – DesperateMateo Jun 03 '21 at 11:28

0 Answers0