Why are you turning your JSON into an array? From what I can tell,
you're just introducing extra overhead...
Re determining the internal key: you've got a couple of options. You
could iterate over the internal object, or you could make the internal
object a hash, and call getKeys() to return all the keys inside it. For
example:
var joResponse = JSON.decode('['+response+']');
console.log(joResponse);
$each(joResponse, function(value, key) {
console.log(value, key);
// Convert the internal object into a hash
(http://mootools.net/docs/core/Native/Hash)
value = $H(value);
// Option 1: Get the keys with a hash
(http://mootools.net/docs/core/Native/Hash#Hash:getKeys)
console.log('keys of the internal object: ', value.getKeys());
// We could now do whatever we like with the keys
// Option 2: Iterate over the internal object
value.each(function(iValue, iKey) { // Hash has an each() method, so we
don't need to use $each()
console.log('\t', iKey, iValue);
});
})
JSONobject.each(function(els){
$each(els, function(value, key){
console.log(key + ': ' + value.isEmpty );
});
});
The question, though, is whether you need to rethink your JSON. I'm guessing
that you're using the 'isEmpty' key as a flag to alert you to something.
Perhaps your internal objects could be represented something like this:
{
message: 'Value is required and cannot be empty',
isEmpty: true
}
That way, your keys are consistently named. You can then check for 'isEmpty'
just by if (value.isEmpty) {} -- if you don't include the isEmpty member, this
will evaluate to false.
Hope this helps.
- Barry van Oudtshoorn
www.barryvan.com.au
On 2/05/2010 7:04 AM, Meroe wrote:
Replying to myself now sorry.
How does this look?
var JSONobject = JSON.decode('['+response+']');
console.log(JSONobject);
JSONobject.each(function(els){
$each(els, function(value, key){
console.log( key + ': ' +
value.isEmpty );
});
});
this outputs the following:
password: Value is required and can't be empty
passwordconfirm: Value is required and can't be empty
costrate: Value is required and can't be empty
firstname: Value is required and can't be empty
lastname: Value is required and can't be empty
username: Value is required and can't be empty
officenumber: Value is required and can't be empty
email: Value is required and can't be empty
Now, what if I don't know what my key is? Anyway to grab that
automatically? would it be value.key or something like that? The key
won't always be isEmpty.
--
Not sent from my Apple πPhone