-1

How to deserialize data values?

{"data": 
    [{
        "Id": 3, 
        "Name" : "Wind", 
        "Type" : 2, 
        "Order" : 1, 
        "user" : "Mike"
    }], 
    "free" : 0, 
    "line" : 10, 
    "count" : 1
}

Here's what I've tried so far:

class Class {
public: 
    virtual ~Class(void);
    virtual void Serialize(Json::Value& root); 
    virtual void Deserialize(Json::Value& root); 

    int Id;
    std::string free;
    std::string line;
    std::string count;
    std::vector<std::string> ID;

};

void Class::Serialize(Json::Value& root) {
    // ...
}

void Class::Deserialize(Json::Value& root) {

     free = root.get("top", 0).asInt();
     line = root.get("skip", 0).asInt();
     count = root.get("count", 0).asInt();

    Json::Value Data= root["data"];
    Id = Data.get("Id", 0).asInt();
}

I cannot deserialize the Data element Id, etc. The elements free, line and count can be extracted.

I have tried to create a second class to separate them, but that would not work either.

sam
  • 9
  • 2

1 Answers1

0

The JSON element data is an array. You missed the index of its element that you want to access.

At the moment, it has only one element so the index would be 0 i.e. Data[0].

Example:

Id = Data[0].get("Id", 0).asInt();
         ^^^
Azeem
  • 11,148
  • 4
  • 27
  • 40
  • Thanks this helped, tried the array index on somethings before and it did not work. – sam May 11 '20 at 17:38