0

I am new to JSON and trying hard to understand it's working.

HTML

<form id="edge" class="form-horizontal" method="post" action="javascript:submit();">
    <input type="text" class="input-xlarge" id="latency" name="latency" placeholder="latency">
    <input type="text" class="input-xlarge" id="throughput" name="throughput" placeholder="throughput">
    <input type="text" class="input-xlarge" id="outUID" name="outUID" placeholder="outUID">
    <input type="text" class="input-xlarge" id="inUID" name="inUID" placeholder="inUID">
    <button type="submit" class="btn btn-success" >Submit Data</button>
</form>

JSON String to be generated as:

{"latency":1.6,"throughput":6.01,"outUID":{"V_ID":"40"},"inUID":{"V_ID":"16"}}

Here's the form and JSON String to be generated

Could some one guide me how do I create that nested JSON object?

2
  • possible duplicate of Convert form data to JS object with jQuery Commented Aug 21, 2013 at 13:30
  • Where are the V_ID key/value pairs coming from in the outUID and inUID objects? Commented Aug 21, 2013 at 13:31

3 Answers 3

1

Since it looks like you want the values of outUID and inUID to be nested for some reason you'll need to build the object manually. Here's a simple example:

var $latency = $('#latency'),
    $throughput = $('#throughput'),
    $outUID = $('#outUID'),
    $inUID = $('#inUID');

var myJSONObject = {
    latency:$latency.val(),
    throughput:$throughput.val(),
    outUID:{
        V_ID:$outUID.val()
    },
    inUID:{
        V_ID:$inUID.val()
    }
};
var stringJSON = JSON.stringify(myJSONObject);
Sign up to request clarification or add additional context in comments.

Comments

0

Pure javascript example

var els=document.getElemtById('edge').getElementsByTagName('input');

or

var els=document.querySelectorAll('input[class=input-"xlarge"]');

to get the elements

then

var array=[]
for(var a=0,b;b=els[a]; ++a){
 array[a]=b.value
}

array is the json object

JSON.styringify(array)

is the json string

tip: if you plan to use this with ajax there is a new way called FormData();

so:

var fd=FormData(document.getElemtById('edge'));

contains the whole form , including files

Comments

0

This code allow you to add more fields if required to do so without hard coding the field attributes

http://jsfiddle.net/6vQY9/

HTML

<form id="edge" class="form-horizontal" method="post" action="javascript:submit();">
    <input type="text" class="input-xlarge" id="latency" name="latency" placeholder="latency">
    <input type="text" class="input-xlarge" id="throughput" name="throughput" placeholder="throughput">
    <input type="text" class="input-xlarge" id="outUID" name="outUID" placeholder="outUID" data-prop="V_ID">
    <input type="text" class="input-xlarge" id="inUID" name="inUID" placeholder="inUID" data-prop="V_ID">
    <button type="submit" class="btn btn-success">Submit Data</button>
</form> <pre></pre>

JS

function submit() {
    var JSONString = "";
    jQuery('#edge input').each(function () {
        var that = jQuery(this);
        var val = that.val();
        var partJSON = "";
        var quote = "\"";
        if (that.data('prop')) {
            partJSON = "{ " + quote +that.data('prop') + quote+ " : " + quote + val + quote + " }";
        } else {
            partJSON = val;
        }
        var comma = that.next('input').length > 0 ? "," : "";

        partJSON = quote + that.prop('id') + quote + ":" + partJSON + comma;
        JSONString += partJSON

    });

    JSONString = "{ " + JSONString + " }";

    jQuery('pre').text(JSONString);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.