jquery map to json string

when use jquery map for collect value to object and string it to json,

you can use  JQUERY map function to collect all become one object and then use JSON.stringify function to convert to JSON string

the problem is, use JSON.stringify function will return “to many” information like this

jquery-map-raw-value

actually value you need just “46” and “5”, or mybe you need json string like {“0″:”46″,”1″:”5”}

this simple function to parse object from JQUERY map to json string will give the valued value


<html>
<head>
<script src="http://localhost/kpk.cmas/application/js/jquery/jquery-1.6.1.min.js" type="text/javascript"></script>
</head>
<body>
<input type='text' class='theinput' value="one">

<input type='text' class='theinput' value="two">

<input type='text' class='theinput' value="third">

<input type='text' class='theinput' value="four">

<button onclick='show_json();'>show json</button>

<script>
function show_json(){
var o= $('.theinput').map(function(){return $(this).val();});

console.log(JSON.stringify(o));

// test output map function
console.log(o.length, o[1]);

var p1 = { numbering:false, zero:true};
var p2 = { numbering:true, zero:true};
var p3 = { numbering:true, zero:false};
var p4 = { zero:false};
var p5 = { zero:true};
var p6 = { numbering:true};
var p7 = { numbering:false};

console.log(obj2json(o));
console.log(obj2json(o,p1));
console.log(obj2json(o,p2));
console.log(obj2json(o,p3));
console.log(obj2json(o,p4));
console.log(obj2json(o,p5));
console.log(obj2json(o,p6));
console.log(obj2json(o,p7));

}
/**
by g3n1k

o is object
p is object
*/
function obj2json(o, p){

if(typeof o === 'object'){

// declare param is object
var param = {
numbering:true, // true => {"1":"val1","2":"val2"} false => {"val1","val2"}
zero:false        // true => {"0":"val1","1":"val2"} false => {"1":"val1","2":"val2"}

};

// merge parameter
if(p) for (var att in p) { param[att] = p[att]; }

// create json string
var a = '', n, l = o.length-1;
o.each(function(i,v) {

n = !param.zero ? i + 1 : i ;

a += (param.numbering ? '"'+ n +'":':'') + '"'+v+'"';

a += i < l ? ',' : '';
});

return "{"+a+"}";
}
return false;
}
</script>
</body>
</html>

and the output

obj2json-string

// by g3n1k =&gt; output json string after JQUERY.map
function obj2json(o, p){
    
    if(typeof o === 'object'){
        
        // declare param is object
        var param = {
                numbering:true, // true = {"1":"val1","2":"val2"} false = {"val1","val2"}
                zero:false        // true = {"0":"val1","1":"val2"} false = {"1":"val1","2":"val2"}
                
            };
        
        // merge parameter
        if(p) for (var att in p) { param[att] = p[att]; }
        
        // create json string
        var a = '', n, l = o.length-1;
        o.each(function(i,v) {
            
            n = !param.zero ? i + 1 : i ;
            
            a += (param.numbering ? '"'+ n +'":':'') + '"'+v+'"';
            
            a += i < l ? ',' : '';
        });
        
        return "{"+a+"}";
    } 
    return false;
}

Published by

G3n1k

just to remember what i had known :)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s