In the case of the prm variable, I am able to reference items using an index such as prm[1].prmdesc. But if I use the eval function to store the JSON string in a variable, I am not able to use that access method. What's more, I haven't been able to find any programmatic way to cycle through the entries.
If I store this to oLoc, I can type:
Code:
alert(oLoc["1"].locdesc)
That will return "Charlotte". But if I setup a for .. next loop:
Code:
for (x=0; x < oLoc.length; x++)
{
alert(oLoc[x].locdesc);
}
It creates an error that it is undefined. If I add another variable s and assign it to be x stored as a string, it is still undefined.
I have also tried for in and this returns only the last entry:
Code:
for (var o in oLoc)
{
alert(oLoc[o].locdesc);
}
When i run that bit of code it displays Test1A and that is all.
I am certain I am missing something basic, but I have not been able to figure out what it is. Can anyone lend a hand?
I'm having a little trouble understanding your problem. You mention a variable "oLoc," but you never provided code that explained how it was defined. Furthermore, I don't understand the following paragraph:
In the case of the prm variable, I am able to reference items using an index such as prm[1].prmdesc. But if I use the eval function to store the JSON string in a variable, I am not able to use that access method. What's more, I haven't been able to find any programmatic way to cycle through the entries.
If "oLoc" is the first snippet of code you posted, you can cycle through it several ways. Following is a simple for loop that alerts the locdesc property of each item.
The code in your example is what I started with, but that produced an undefined error. I have gotten it to work by placing the code to cycle through the array in its own function and I pass the array to it. It works fine like that, but I can not get it to work without separating the code into its own function. It just seems strange to me. I have double checked that no variables are being reused inside the loop.
Can you post the code? I'm having trouble visualizing exactly what your program looks like at this point.
You might try running some tests against oLoc to see what JavaScript is doing with it. Alert the variable, and then alert the typeof oLoc and see if it returns "string," "object," etc. Since arrays are objects in JavaScript, you should be getting the result "object." If you're getting string, that's where your problem is.
Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.
Bookmarks