/*
* Returns the token for the value associated with the specified key
*/
jsmntok_t* JsonHashTable::getToken(const char* desiredKey)
{	
	// sanity check
	if (json == 0 || tokens == 0 || desiredKey == 0)
		return 0;

	// skip first token, it's the whole object
	jsmntok_t* currentToken = tokens + 1;

	// scan each keys
	for (int i = 0; i < tokens[0].size / 2 ; i++)
	{
		// get key token string
		char* key = getStringFromToken(currentToken);

		// compare with desired name
		if (strcmp(desiredKey, key) == 0)
		{
			// return the value token that follows the key token
			return currentToken + 1;
		}

		// move forward: key + value + nested tokens
		currentToken += 2 + getNestedTokenCount(currentToken + 1);
	}

	// nothing found, return NULL
	return 0; 
}
int JsonObjectBase::getNestedTokenCount(jsmntok_t* token)
{
	int count = 0;

	for (int i = 0; i < token->size; i++)
	{
		count += 1 + getNestedTokenCount(token + 1 + i);
	}

	return count;
}
Ejemplo n.º 3
0
/*
* Returns the token for the value at the specified index
*/
jsmntok_t* JsonArray::getToken(int index)
{
    // sanity check
    if (json == 0 || tokens == 0 || index < 0 || index >= tokens[0].size)
        return 0;

    // skip first token, it's the whole object
    jsmntok_t* currentToken = tokens + 1;

    // skip all tokens before the specified index
    for (int i = 0; i < index; i++)
    {
        // move forward: current + nested tokens
        currentToken += 1 + getNestedTokenCount(currentToken);
    }

    return currentToken;
}