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 3 of 3
  1. #1
    Regular Coder
    Join Date
    May 2009
    Posts
    164
    Thanks
    75
    Thanked 1 Time in 1 Post

    Creating JSON from input

    Hi guys, have a bit of a problem. I have a simple form
    Code:
    <form action="" method="post">
        <div>
            <label for="origin">Origin:</label>
            <input type="text" name="origin" id="origin" />
        </div>
        <div>
            <label for="destination">Destination:</label>
            <input type="text" name="destination" id="destination" />
        </div>
        <div>
            <label for="date">Depart:</label>
            <input type="text" name="date" id="date" />
        </div>
        <div>
            <label for="datepicker2">Return:</label>
            <input type="text" name="return" id="datepicker2" />
        </div>
        <div>
            <label for="preferredCabin">Class:</label>
            <input type="text" name="preferredCabin" id="preferredCabin" />
        </div>
        <div class="button">
            <button type="submit">Search for flights</button>
        </div>
    </form>
    What I am trying to do is take that input, and inject it into a JSON file. The JSON I need to produce from the above is
    Code:
    {
      "request": {
        "passengers": {
          "adultCount": 1
        },
        "slice": [
          {
            "origin": {origin},
            "destination": {destination},
            "date": {date},
    	"preferredCabin": {preferredCabin}
          },
          {
            "origin": {destination},
            "destination": {origin},
            "date": {depart},
    	"preferredCabin": {preferredCabin}
          }
        ],
    	"solutions": 1
      }
    }
    So the variables in {} are the ones I need to inject. Ones like solutions are hardcoded so can stay as above. At the moment I am trying to just recreate a JSON file because I dont know if I can inject directly into an already existing one. So I am doing
    Code:
    $(function(){
        $.fn.serializeObject = function()
        {
            var o = {};
            var a = this.serializeArray();
            $.each(a, function() {
                if (o[this.name] !== undefined) {
                    if (!o[this.name].push) {
                        o[this.name] = [o[this.name]];
                    }
                    o[this.name].push(this.value || '');
                } else {
                    o[this.name] = this.value || '';
                }
            });
            return o;
        };
    
        $(function() {
            $('form').submit(function() {
                $('#result').text(JSON.stringify($('form').serializeObject()));
                return false;
            });
        });
    });
    This currently returns
    Code:
    {"origin":"LON","destination":"LAX","date":"06/26/2014","return":"06/27/2014","preferredCabin":"BUSINESS"}
    So its getting there, not quite correct though, and I am not to sure how to start of with request and then do the slice part.

    So my question is what is the best way of getting my inputted variables into my JSON file?

    Thanks

  • #2
    New Coder
    Join Date
    Jun 2014
    Posts
    13
    Thanks
    0
    Thanked 1 Time in 1 Post
    This can be done easily with php or do you strictly want javascript? Let me know...

  • Users who have thanked ronniej403 for this post:

    nick2price (06-25-2014)

  • #3
    Regular Coder
    Join Date
    May 2009
    Posts
    164
    Thanks
    75
    Thanked 1 Time in 1 Post
    Hi, I didnt even look into PHP. Looking at it now, seems like PHP would be the logical approach. Modes feel free to move this topic.


  •  

    Posting Permissions

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