accessing JSON data

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • elvehjem
    New Member
    • Apr 2010
    • 8

    accessing JSON data

    Hi,

    I'm trying to learn JavaScript. I'm missing something that has to do w/ reading stuff from (what I hope is) a JSON data structure.

    This function:

    Code:
    function myFunction(){					
    outputter = document.getElementById("outputter");
    outputter.innerHTML = "";
    for (var z in Township) {
    outputter.innerHTML += "   Z = " + z + "  " + "<br>";
    for (var w in Township[z]) {
    outputter.innerHTML += "   w = " + w + "  ="  + Township.z
    	}	}	}
    Does not give any errors, but it also does not yield what I expect.

    Here's the data:
    Code:
    var Township = {
    	
    	'Boon Lake' :	[
    		{'abbr' : 'BLK', 'SId' : 16, 'lat' : 44.35176, 'long' : -93.19764	}, 
    		{'abbr' : 'BLK', 'SId' : 17, 'lat' : 44.35276, 'long' : -93.18764	}
    						],
    	'Sacred Heart' :[ 
    		{'abbr' : 'BLK', 'SId' : 16, 'lat' : 44.35176, 'long' : -93.19764	}, 
    		{'abbr' : 'BLK', 'SId' : 17, 'lat' : 44.35276, 'long' : -93.18764	}
    						 ]
    						 };
    Would appreciate if someone can tell me why I can't access the data, esp. the "abbr" field. (I realize my code doesn't reflect that, but the idea is that want to retrieve *all* the data.

    Thanks!
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    so, what does it output and what should it output?

    Comment

    • elvehjem
      New Member
      • Apr 2010
      • 8

      #3
      Originally posted by Dormilich
      so, what does it output and what should it output?
      thanks for the reply, and "Good Question!"

      This line:
      Code:
      outputter.innerHTML += "   w = " + w + "  ="  + Township.z
      prints the quoted text, as expected, and the value of "w" (either 0 or 1) which I didn't expect, exactly, and the name of the township, (Boon Lake or Sacred Heart) as they appear in the data.

      This is kind of a test bench script, and I've changed the heck out of it before and after I sent it, but essentially, I am trying to prove that I can access the data in the JSON file; therefore, if I could find a way to print *all* the data, brick by brick, that would be fine.

      I am not convinced that my data is in the right format for doing that. I can *make* the JSON file look any how I want... or need... to have it look for the script to read it.

      I hoped that my intent was clear even if my format was messed up: I have 2 townships, each with 2 points of interest(16 & 17) and some *attributes* about those points.

      So, my data is there, and correct, but may be in the wrong format, and my script will, I hope, and with some other folks help, be able to read the data. I'd like to store it as JSON data because I have a great deal of it, most of which will not be of interest to a particular user, but the data that they are interested in, I'd like to cache locally as a cookie, or if there is some other better way, I'd jump all over it.

      Thanks again for taking time to read my post and reply!

      Ken

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        and the value of "w" (either 0 or 1) which I didn't expect
        why that? z is an array, so naturally its elements (w) are 0 and 1.

        Comment

        • elvehjem
          New Member
          • Apr 2010
          • 8

          #5
          Well, yes, I did expect the variable w to resolve to a 0 or 1.

          I guess what I'm getting at are 2 things:

          1. Is my data in a valid format? and
          2, Given that it is, how do I access one element in it, say where SId =16 in Boon Lake?

          I suspect that, as usual, I have missed some vital piece of info, be it a trick, or procedure, that I need to know to understand how to make my output line include that data. I hope from my code (even if faulty) that you can tell that I am trying to do a nested loop to access the data. Maybe that isn't necessary-- I just can't tell; and I'm hoping for some help along those lines.

          Ken

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            you have to use another for … in loop and an if condition to test the SId.

            as far as I can see the code is valid.

            Comment

            • elvehjem
              New Member
              • Apr 2010
              • 8

              #7
              OK, thanks for that.

              I will be testing that shortly.

              Ken

              Comment

              • elvehjem
                New Member
                • Apr 2010
                • 8

                #8
                Well, that did the trick. I had wondered if I needed the third loop, because I saw that I had 3 layers of info, but I had also thought I could get it by reference w/o the loop.

                Thanks so much for your help!

                Ken

                Comment

                Working...