JSON/Array Question

  • Follow


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[i].name == 'error') {
    //do stuff...
  }
}

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

Any help would be appreciated.

0
Reply tcole6 (46) 3/6/2007 9:48:02 PM

Tom Cole wrote:
> 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[i].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
0
Reply Jeremy 3/7/2007 1:56:55 AM


On Mar 7, 7:48 am, "Tom Cole" <tco...@gmail.com> wrote:
> 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[i].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[i]
    }
  } while(i)

No promises though.  :-)


--
Rob

0
Reply RobG 3/7/2007 2:43:40 AM

Tom Cole <tcole6@gmail.com> wrote:

> for (var i = 0; i < object.values.length; i++) {
>   if (object.values[i].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[i]).name === 'error)...

parenthesing object.values[i] ?

but, as suggested by other, u might change your object structure to
object of object rather than object of array.
-- 
Une B�vue
0
Reply unbewusst 3/7/2007 5:01:20 AM

On Mar 6, 8:56 pm, Jeremy <jer...@pinacol.com> wrote:
> Tom Cole wrote:
> > 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[i].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[i].name);
  if (element) {
     //this is just an example...
     element.value = object.values[i].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.

0
Reply Tom 3/7/2007 2:01:54 PM

Tom Cole wrote:
> 
> 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[i].name);
>   if (element) {
>      //this is just an example...
>      element.value = object.values[i].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

0
Reply Jeremy 3/7/2007 5:13:37 PM

5 Replies
104 Views

(page loaded in 0.089 seconds)


Reply: