0

I am building a table tennis score board which parses json objects with ID and names of players to let the players pick their name inside a menu and later on submit the scores to my server. But I am stuck throwing fetching names and IDs, troughing them in an array and passing it to my menu library.

I tried looping through the objects and collecting them into a structure so, I can pass them as arrays. But my attempts so far are failing miserably.

void ConnectionClass::parsePlayerList()
{
if (connectionWifiStatus == 1) {

// connect to http server
WiFiClient client;
IPAddress host(127, 0, 0, 1);   // Server IP address
client.connect(host, 80);

// Send HTTP request
client.println("GET /index.php?id=15&type=100"); // URL to fetch list from
client.println("localhost");    // Hostname
client.println("Connection: close");
client.println();
if (client.println() == 0) {
    return; // Http request failed
}

// Skip response headers
char endOfHeaders[] = "\r\n\r\n";
client.find(endOfHeaders);

// Allocate JsonBuffer
DynamicJsonBuffer playersBuffer(3200);  // Reserve memory for buffer in kb

// Parse JSON object
JsonArray& root = playersBuffer.parseArray(client);
if (!root.success()) {
    return;     // parseObject() failed
}
else {
    // Extract information
    int8_t jsonElements = root.size();  // Get size of json array

    structPlayer player[root.size];
    for (int8_t i = 0; i < jsonElements; i++)
    {
        player.playerUID = root[i]["uid"].as<int>();    // Fill UIDs
        player.playerName = root[i]["name"].as<char>(); // Fill with names 

        playerUID[i] = root[i]["uid"].as<int>();
        playerName[i] = root[i]["name"].as<char>();
    }
}

// Disconnect
client.stop();
};
}

I thought a struct would meet be a good way to conquer my problem, but any other solution that let's me access values by index will do. Since I'm not that experienced in programming any other approach is welcome too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.