JSON/Array Question

T

Tom Cole

Let's say I have the following JSON string returned from a server-side
process:

{ values: [{"name": "value1", "value": "1"},{"name": "value2",
"value": "0"},{"name": "operand", "value": "/"},{"name": "result",
"value": "NaN"},{"name": "error", "value": "Divide by 0"}] }

I then create a JSON object from it using eval() (tell me if this is
not what should be done).

My question is this...how do I determine (without going through every
single record) if there is a name=error in the values array?

Here's what I do now, it can't be optimum:

var object = eval('{ values: [{"name": "value1", "value": "1"},
{"name": "value2", "value": "0"},{"name": "operand", "value": "/"},
{"name": "result", "value": "NaN"},{"name": "error", "value": "Divide
by 0"}] }');

for (var i = 0; i < object.values.length; i++) {
if (object.values.name == 'error') {
//do stuff...
}
}

I thought I'd try object.values.name['error'] of course that didn't
work...

Any help would be appreciated.
 
J

Jeremy

Tom said:
Let's say I have the following JSON string returned from a server-side
process:

{ values: [{"name": "value1", "value": "1"},{"name": "value2",
"value": "0"},{"name": "operand", "value": "/"},{"name": "result",
"value": "NaN"},{"name": "error", "value": "Divide by 0"}] }

I then create a JSON object from it using eval() (tell me if this is
not what should be done).

My question is this...how do I determine (without going through every
single record) if there is a name=error in the values array?

Here's what I do now, it can't be optimum:

var object = eval('{ values: [{"name": "value1", "value": "1"},
{"name": "value2", "value": "0"},{"name": "operand", "value": "/"},
{"name": "result", "value": "NaN"},{"name": "error", "value": "Divide
by 0"}] }');

for (var i = 0; i < object.values.length; i++) {
if (object.values.name == 'error') {
//do stuff...
}
}

I thought I'd try object.values.name['error'] of course that didn't
work...

Any help would be appreciated.


Use a magical instant search algorithm.

Or, come up with a better way of returning your data :-D

In all likelyhood, it takes a negligible amount of time to go through
the whole array, unless there are a lot of entries. But it looks to me
like this might be better suited as an object, just from the example you
gave. If there are duplicate "name"s, then I'm wrong, but how about:

{
value1: 1,
value2: 0,
operand: '/',
result: 'NaN',
error: 'Divide by 0'
)

using an object instead of an array of objects that is basically a flat
object in a poorly organized table. Then you could just do

if(typeof(object.error) != "undefined")
//blah blah

Or something like that.

Jeremy
 
R

RobG

Let's say I have the following JSON string returned from a server-side
process:

{ values: [{"name": "value1", "value": "1"},{"name": "value2",
"value": "0"},{"name": "operand", "value": "/"},{"name": "result",
"value": "NaN"},{"name": "error", "value": "Divide by 0"}] }

I then create a JSON object from it using eval() (tell me if this is
not what should be done).

My question is this...how do I determine (without going through every
single record) if there is a name=error in the values array?

Your object has a single property whose value is an array of objects -
so your response object is essentially an array. You need to loop
over all the elements of the array to get the 'name' property of the
objects, there is no other way.

You might consider:

- a different data format
- include an index object to help with searching (say
an array of the indices where name='error')
- sort before sending so objects with name='error' are
first (or last and search backwards) so you can stop
when your search finds the first non-error object.

The above may not provide much practical benefit if the array isn't
large (say less than 1,000 elements).

Here's what I do now, it can't be optimum:

var object = eval('{ values: [{"name": "value1", "value": "1"},
{"name": "value2", "value": "0"},{"name": "operand", "value": "/"},
{"name": "result", "value": "NaN"},{"name": "error", "value": "Divide
by 0"}] }');

for (var i = 0; i < object.values.length; i++) {
if (object.values.name == 'error') {
//do stuff...
}


You could optimise that somewhat with:

var o = object.values;
var i = o.length;
do {
if (o[--i].name == 'error') {
// do stuff with o
}
} while(i)

No promises though. :)
 
U

Une Bévue

Tom Cole said:
for (var i = 0; i < object.values.length; i++) {
if (object.values.name == 'error') {
//do stuff...
}
}

I thought I'd try object.values.name['error'] of course that didn't
work...


strange, it is correct, did you try :

if((object.values).name === 'error)...

parenthesing object.values ?

but, as suggested by other, u might change your object structure to
object of object rather than object of array.
 
T

Tom Cole

Tom said:
Let's say I have the following JSON string returned from a server-side
process:
{ values: [{"name": "value1", "value": "1"},{"name": "value2",
"value": "0"},{"name": "operand", "value": "/"},{"name": "result",
"value": "NaN"},{"name": "error", "value": "Divide by 0"}] }
I then create a JSON object from it using eval() (tell me if this is
not what should be done).
My question is this...how do I determine (without going through every
single record) if there is a name=error in the values array?
Here's what I do now, it can't be optimum:
var object = eval('{ values: [{"name": "value1", "value": "1"},
{"name": "value2", "value": "0"},{"name": "operand", "value": "/"},
{"name": "result", "value": "NaN"},{"name": "error", "value": "Divide
by 0"}] }');
for (var i = 0; i < object.values.length; i++) {
if (object.values.name == 'error') {
//do stuff...
}
}

I thought I'd try object.values.name['error'] of course that didn't
work...
Any help would be appreciated.

Use a magical instant search algorithm.

Or, come up with a better way of returning your data :-D

In all likelyhood, it takes a negligible amount of time to go through
the whole array, unless there are a lot of entries. But it looks to me
like this might be better suited as an object, just from the example you
gave. If there are duplicate "name"s, then I'm wrong, but how about:

{
value1: 1,
value2: 0,
operand: '/',
result: 'NaN',
error: 'Divide by 0'
)

using an object instead of an array of objects that is basically a flat
object in a poorly organized table. Then you could just do

if(typeof(object.error) != "undefined")
//blah blah

Or something like that.

Jeremy- Hide quoted text -

- Show quoted text -


I would love to do that, but what I'm writing is a method to
automatically populate a form with the "values" and I need a way to
randomly access them. Something like:

for (var i = 0; i < object.values.length; i++) {
var element = document.getElementById(object.values.name);
if (element) {
//this is just an example...
element.value = object.values.value;
}
}

For this application, my data structure works well (for me). There is
just a unique situation where I'm looking for the absence of a "name"
that I was hoping for a shortcut.

Thanks for your assistance. I'm just starting with JSON and am only
working with it in my spare time.
 
J

Jeremy

Tom said:
I would love to do that, but what I'm writing is a method to
automatically populate a form with the "values" and I need a way to
randomly access them. Something like:

for (var i = 0; i < object.values.length; i++) {
var element = document.getElementById(object.values.name);
if (element) {
//this is just an example...
element.value = object.values.value;
}
}

<snip>


I still think you should use an object. You basically have name-value
pairs that you are returning, where the name is unique, and must conform
to HTML identifier standards (because you are also using the name as an
HTML form element name). This is perfect for an object. Instead you
are using an array of name-value pair objects, which makes no sense.

From your last post, I gather that you are doing this because you think
this is the only way you can loop through the names/values, and have
access to both the name and value as strings. This is not the case:

[Example]

var object = {
value1: 1,
value2: 0,
operand: '/',
result: 'NaN',
error: 'Divide by 0'
};

var myform = document.getElementById("MyForm");
// or document.forms["MyForm"] if you prefer using name instead of id

for(var name in object)
{
//alerts, for example, "value1: 0"
alert(name + ": " + object[name]);

//sets form value to value from object
if(typeof(myform.elements[name]) != "undefined")
myform.elements[name].value = object[name];

}

[/Example]

In this example, if you have a form with elements named "value1",
"value2", "operand", and "result", for example, then those form elements
will be repopulated with the values from the returned object.

Give it some thought :)

Jeremy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,232
Latest member
DorotheaDo

Latest Threads

Top