473,320 Members | 1,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

JSON/Array Question

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.

Mar 6 '07 #1
5 34371
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
Mar 7 '07 #2
On Mar 7, 7:48 am, "Tom Cole" <tco...@gmail.comwrote:
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

Mar 7 '07 #3
Tom Cole <tc****@gmail.comwrote:
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
Mar 7 '07 #4
On Mar 6, 8:56 pm, Jeremy <jer...@pinacol.comwrote:
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.

Mar 7 '07 #5
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

Mar 7 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Luke Matuszewski | last post by:
Welcome As suggested i looked into JSON project and was amazed but... What about cyclical data structures - anybody was faced it in some project ? Is there any satisactional recomendation... ...
2
by: Kevin Newman | last post by:
Hello, I noticed that the JavaScript library for JSON posted on json.org (http://www.json.org/json.js) is modifying Object.prototype (adding a method - toJSONString). I thought this was...
1
by: kungfumike | last post by:
Currently having a problem getting JSON to include properly. I When I attempt to call .toJSONstring on an array it was giving me an error. So I resorted to just alerting the array out to the screen...
4
RMWChaos
by: RMWChaos | last post by:
The next episode in the continuing saga of trying to develop a modular, automated DOM create and remove script asks the question, "Where should I put this code?" Alright, here's the story: with a...
4
by: im12345 | last post by:
I have the following question: Im doing a sample application using dojo and json. I have 2 classes: 1. Book class package com.esolaria.dojoex; import org.json.JSONObject; import...
5
by: Jeff | last post by:
Lets say we have what I would call a "hash": var HASH =new Array(); HASH='first'; HASH='second'; HASH='third'; I'd like to return that as JSON data. Is there a direct way to do that?
7
by: Logos | last post by:
I am using PHP with the JSON extension function json_decode. I have a JSON with a member named "1" (ie) { "1":"somedata" } Trying to access this via the -operator doesn't work, nor does . ...
9
by: Jon Paal [MSMD] | last post by:
using json like ( {"Records": , "RecordCount":"1" } ) and jquery like: $.ajax({ .... success: function(json, status) {
0
by: crocodilu2008 | last post by:
JSON vs. XML JSON and XML are basically used for the same purpose—to represent and interchange data. I'll try to show you why you might want to use JSON rather than XML in an AJAX context by showing...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.