473,796 Members | 2,460 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Iterate through JSON property list

RMWChaos
137 New Member
Any JSON experts out there? I'd like to know if it is possible, and if so how, to iterate through a JSON property list so that each iteration selects the next value for each object. Here is an example list:

Expand|Select|Wrap|Line Numbers
  1. function myFunction()
  2. {
  3.     createDOM({
  4.     'id'      :   ["formname", "inputname", "submitbutton"],
  5.     'dom'     :   ["form", "input", "input"],
  6.     'parent'  :   "content",
  7.     'form'    :   [null, "formname", "formname"],
  8.     'action'  :   [URI, null, null],
  9.     'method'  :   ["post", null, null],
  10.     'type'    :   [null, null, "submit"],
  11.     'size'    :   [null, 6, null],
  12.     'value'   :   [null, null, "Submit"],
  13.     )};
  14. };
  15.  
Now what I want to do is iterate through this list 3 times, in each case selecting the next value for each object. So through each iteration, it will go through all the objects (id, dom, parent, etc.). On the first run it will select the first set of values for each object (formname, form, content, null, URI, etc.). On the second run, it will select the second set (inputname, input, content, formlogin, etc.) and so on. In the case of the 'parent' object, it will select 'content' on each run.

I know one way to do this, but it involves creating an array for each object's value list, like so:

Expand|Select|Wrap|Line Numbers
  1. function myFunction()
  2. {
  3. var A = new Array ("formname", "inputname", "submitbutton");
  4. var B = new Array ("form", "input", "input");
  5. var C = new Array(null, "formname", "formname");
  6. var x = 0;
  7. var D = A.length + x;
  8. var E = B.length + x;
  9. var F = C.length + x;
  10.  
  11. for (x <= 3; x >= 0; x++)
  12.     {
  13.     createDOM({
  14.     'id'      :   D,
  15.     'dom'     :   E,
  16.     'parent'  :   F,
  17.     )};
  18. };
  19.  
That would result in a long list of arrays and "array.leng th + x" vars. So I am hoping there is an easier way by working with the first example.

Thanks!
Nov 13 '07
25 25453
gits
5,390 Recognized Expert Moderator Expert
I do have one other question, however. You have the exceptions in a JSON list, so I copied what you did, but is that necessary? Why not just:

Expand|Select|Wrap|Line Numbers
  1. var exceptions = new Array ("onclick", "onmouseover", "onmouseout");
  2.  
There is no data to hold here; so I guess I am missing the point of a JSON property list.

Thanks!
it's not really called a json-list ... its a simple javascript object. have a look at the following simple usage:

Expand|Select|Wrap|Line Numbers
  1. if (index in exceptions) {
  2.    // some code
  3. }
  4.  
that wouldn't be possible with an array :) where you would always have to loop through when checking a value. so the current code simply avoids that. and one more note ... simply to mention :) ...

Expand|Select|Wrap|Line Numbers
  1. // create an array-instance
  2. var arr = new Array();
  3.  
  4. // equivalent to this :: but js needn't to evaluate
  5. // the brackets
  6. var arr = new Array;
  7.  
  8. // equivalent to this :: more less to evaluate :) through
  9. // the use of literals
  10. var arr = [];
  11.  
kind regards
Nov 23 '07 #21
RMWChaos
137 New Member
Hey gits,

Yeah, I tried it out, and it didn't work. =\

Sometimes, it's the only way to learn.

Thanks!
Nov 23 '07 #22
RMWChaos
137 New Member
I asked why this:
Expand|Select|Wrap|Line Numbers
  1. /**
  2. * @private
  3. */ 
  4.  
You replied:
that's for marking, that you shouldn't use that method seperatly ... only set_node_attr() ; should be used in the code. the reason for that is, that you later on simply may change the functions without changing the code in different places, and so centralizes the attrib/property setting in one place.
I'm not quite sure what you mean. Maybe it would be easier if I learned Deutsch. =D I think you mean by using that code snippet, it forces all functions that come after it to only be used in conjunction with the function before it, or is it just a reminder to the programmer?

As for util-method...Ok, ok, ok, you win! I will make these changes:

Expand|Select|Wrap|Line Numbers
  1. //in changeAttrib
  2. function changeAttrib(id, attr, value))
  3.      {
  4.      set_node_attrib(id, attr, value, false);
  5.      };
  6.  
  7. // in createDOM()
  8. for (index in attrib)
  9.      {
  10.      set_node_attrib(attrib.id, index, attrib[index], true);
  11.      };
  12.  
  13. // in set_node_attrib
  14. function set_node_attr(id, attr, value, state)
  15.      {
  16.      var node = docroot.getElementById(id);
  17.      var exceptions = 
  18.           {
  19.           'onclick'     :   1,
  20.           'onmouseover'    :   1,
  21.           'onmouseout'    :   1
  22.           };
  23.      if (attr in exceptions)
  24.           {
  25.           set_node_exceptions();
  26.           }
  27.      else if (state)
  28.         {
  29.         node.setAttribute(attr, value);
  30.         }
  31.      else
  32.           {
  33.           node.removeAttribute(attr);
  34.           };
  35.      };
  36.  
  37. /**
  38. * @private
  39. */
  40.  
  41. function set_node_exceptions(node, attr, value)
  42.      {
  43.      node[attr] = value;
  44.      };
  45.  
Nov 23 '07 #23
gits
5,390 Recognized Expert Moderator Expert
it's a reminder as you said ... and the programmer should pay attention to it :) it's always a good idea to handle things that mix up different things like our attribute/property example in one place ... respectively with one single method-call ... so that everytime you have to build in more exeptions you have only one point where you have to maintain the code ;) ...

kind regards
Nov 23 '07 #24
RMWChaos
137 New Member
it's always a good idea to handle things that mix up different things like our attribute/property example in one place ... respectively with one single method-call ... so that every time you have to build in more exceptions you have only one point where you have to maintain the code
I understand exactly what you are saying, but I have a problem in using this code. The util-method assumes that the node-id already exists, but in the case of my createDOM script, it does not. The attribs are being set immediately before the node is actually created, so that the node is created with the attribs. Hm, let me see if I can create the node, then set the attribs. That might work...testing. ..

Nope, didn't work. Again, the problem is that no attribs have been set, including the node id, so that document.getEle mentById(id) has nothing to find. If I am going to use your util-method, I really need to give this some thought.
Nov 23 '07 #25
gits
5,390 Recognized Expert Moderator Expert
simply change it to pass the node itself to it instead of the id :)

kind regards
Nov 23 '07 #26

Sign in to post your reply or Sign up for a free account.

Similar topics

2
5239
by: Jim Heavey | last post by:
I want to write a routine which will list all of the propetries and all property names for a particular object, say a DataColumn. I there a way for me to do this without manually looking up each property name and the property value? Thanks in advance for your assistance!!!!!!!!!!!
7
1698
by: Raymond Lewallen | last post by:
Want to know if/how to get a list of properties that are available in a class and store the properties names in an arraylist. TIA, Raymond Lewallen
0
1062
by: zacks | last post by:
I have a class property that is a List of another class. I would like to add an instance to this property via the Type.InvokeMethod method. But when I try invoke the method with a bing flag of SetProperty I get a Missing Method exception. Is it possible to add an instance to a property List via InvokeMethod?
3
16530
by: Steve | last post by:
Hello- I have numerous variables called varNameX (where X is equal to a number from 0 to a known number "n"). Using VB.NET, I would like to iterate through all of these variables to run comparisons. Example:
15
7731
RMWChaos
by: RMWChaos | last post by:
As usual, an overly-long, overly-explanatory post. Better too much info than too little, right? A couple weeks ago, I asked for some assistance iterating through a JSON property list so that my code would either select the next value in the member list or the single value. The original post can be found here. This is the code gits helped me write: for (var i = 0; i < attribList.id.length; i++) { var attrib = {};
4
2823
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 great deal of help from gits, I've developed a DOM creation and deletion script, which can be used in multiple applications. You simply feed the script a JSON list of any size, and the script will create multiple DOM elements with as many attributes...
3
3828
by: Bitslong | last post by:
I have the following JSON obj/code: var json_logs = { "countries": , "US":{"num_calls":"555","time":"432"}, "AU":{"num_calls":"212","time":"233"} } var call = json_logs.evalJSON();
6
1467
by: Academia | last post by:
VS2008 The help for Form lists ContextMenu as a property but it does not appear in the properties for my form. Is there something I must do to make it appear there. Thanks
3
2349
by: Tyro | last post by:
I have built a database that is updated daily with new items. Each new item needs to be given to someone on our team to work on. I would like to automatically assign the new items to the team members identified on a list and iterate through the list so that the work is distributed evenly. Thanks for any help!
0
9683
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9529
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10457
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10231
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10013
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6792
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5576
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3733
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2927
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.