0

I am not so experienced in programming in C and I occurred a problem.

I'm trying to pass cJSON* body_json to a function int _parseJSON(const char* const body, cJSON* body_json) via pointer. In this function, I want to change body_json and return it via this pointer, but the returned value is incorrect (I have errors in the next function that's using this body_json).

Code:

static int _parseJSON(const char* const body, cJSON* body_json)
{
    body_json = cJSON_Parse(body);
    if (body_json == NULL)
    {
        // Couldn't parse body to JSON. Probably wrong format given or no data.
        const char* error_ptr = cJSON_GetErrorPtr();
        if (error_ptr != NULL)
        {
            ESP_LOGE("Error in JSON string: %s", error_ptr);
        }
        cJSON_Delete(body_json);
        return -1;
    }
    else{
        return 0;
    }

}

int parse_feedback(const char * const body, FeedbackRequest *feedback_data_request)
{
    int status=0;
    const char* email_key = "email";

    // Parse body to json
    cJSON* body_json=NULL;
    status = _parseJSON(body, &body_json);

    status = _parseString(body_json, &(feedback_data_request->email), email_key);

return status;
}
4

0 回答 0