Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Nov 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Dynamically Creating JSON

    Hi all,

    I have a form with multiple fields (around 50) which I need to store to sql database. I have some nice php code that can take my json id value pairs and add it to the correct fields in the mysql database.

    All I need to give it is valid json and it does the rest. My problem is creating json for this large collection of fields without doing it manually.

    So my json object needs to be made from dom id, value pairs from nodes with classname input.

    # I can get this list of input fields like this

    var list=dojo.query(".input");

    # I can then get their ids and values in a loop like this

    for (var i=0;i<list.length;i++)
    {
    var id=list[i].id;
    var value=list[i].value;
    }

    How can I create an object of the following structure, using the loop above or is it possible?

    var data={
    orderer_id: dojo.byId("orderer_id").value,
    final_comments_id: dojo.byId("final_comments_id").value
    ...,
    ...
    };

    If I can create this data object on the fly without manually specifying any fields in it, I can then use the method below to convert it to perfect json.

    var json_of_data=dojo.toJson(data);

    Thanks for any suggestions,
    Mark

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,379
    Thanks
    11
    Thanked 592 Times in 572 Posts
    repetitive data gets an array of simply objects:
    Code:
    var list=dojo.query(".input");
    var dynOb = [];
    
    for (var i=0;i<list.length;i++){
      var data={
         orderer_id:list[i].id,
         final_comments_id: list[i].value
      };
      dynOb[i] = data;
    }
    Last edited by rnd me; 11-25-2008 at 01:03 PM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • #3
    New to the CF scene
    Join Date
    Nov 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    what i need in my data object is what needs to be created dynamically, both values and variable names so

    data={
    var_name:value_name;
    var_name:value_name;
    var_name:value_name;
    var_name:value_name;
    var_name:value_name;
    var_name:value_name;
    var_name:value_name;
    var_name:value_name;
    var_name:value_name;
    }

    It is these "var_name"' s that I will be getting from the dojo.query(), and the value's also.

    I do not know how to dynamically add different variables to the data object. Any ideas how I could manage this?

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,379
    Thanks
    11
    Thanked 592 Times in 572 Posts
    ahh, i see.

    something along the lines of this should work:

    Code:
    var data = {};
    
    for loop begin
        data[list[i].id] = list[i].value
    for loop end
    make sense?
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • #5
    New to the CF scene
    Join Date
    Nov 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Amazingly easy! Thanks so much!

    I love JSON


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •