PDA

View Full Version : display the objects in an array in some json with toSourceCode function


TinyScript
04-15-2010, 06:23 PM
Hi everyone, I'm trying to modify this script to print out the objects in the array when it prints the rest of the json.
this is the original example as found here
http://phrogz.net/js/hasOwnProperty.html

//Simplistic version for illustration purposes only
Object.prototype.toSourceCode = function( )
{
var theSourceCode = '{\n';
for ( var thePropName in this )
{
if ( !this.hasOwnProperty || this.hasOwnProperty( thePropName ) )
{
theSourceCode += ' ' + thePropName + ' : ' + this[ thePropName ] + ',\n';
}
}
return theSourceCode.replace( /,\n$/, '\n}' );
}

var theHash = { size:12, count:53, tax:0.06, sku:8295193 };

document.write( theHash.toSourceCode( ) );





This is my code with the json I want to display. I removed my attempts because they simply made more problems than they solved, but I assure you, I've tried for a few weeks to make this work. It would be nice to have this because the json.stringify() function doesn't work with IE.

What I need to figure out how to do is test the contents of this[prop] and if it has more stuff in there, I want to print it also.

I tried for...in loops, for loops, while loops, and even did test for typeof=="object", !="string",... etc as I was trying everything I coukd think of. All of these were combined with a hasOwnProperty test in the loop. I would just end up printing all the letters for each string and each loop would print all the array objects, so there'd be way too many.

Any guidence would be helpful.

<head></head>
<body>
<script type="text/javascript">





Object.prototype.toSourceCode=function(){

var theSourceCode = '{\n';

for ( var prop in this ){

if (this.hasOwnProperty(prop)){


theSourceCode += prop+":"+this[prop] + ",\n" }

}

return theSourceCode.replace(/,\n$/, "\n}" )




}


var info={
"id":"Id",
"label":"name",
"desc":"description",
"amnt":"amount",
"date":"date",
"items":[
{"Id" : "materials","name" : "supplies","description" : "filler","amount" : "44","date" : "111209"},
{"Id" : "materials","name" : "supplies","description" : "eastman","amount" : "30.71","date" : "101209"}
]
}
window.onload=function(){
document.getElementById("readout").innerHTML=info.toSourceCode()
}
</script>
<div id="readout">
</div>
</body>
</html>

rnd me
04-16-2010, 12:42 AM
I've tried for a few weeks to make this work. It would be nice to have this because the json.stringify() function doesn't work with IE.

sure it does:
//JSON:
if(!this.JSON){this.JSON={}}(function(){function l(c){return c<10?"0"+c:c}if(typeof Date.prototype.toJSON!=="function"){Date.prototype.toJSON=function(c){return isFinite(this.valueOf())?this.getUTCFullYear()+"-"+l(this.getUTCMonth()+1)+"-"+l(this.getUTCDate())+"T"+l(this.getUTCHours())+":"+l(this.getUTCMinutes())+":"+l(this.getUTCSeconds())+"Z":null};String.prototype.toJSON=Number.prototype.toJSON=Boolean.prototype.toJSON=function(c){return this.valueOf()}}var o=/[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,p=/[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,h,m,r={'\b':"\\b",'\t':"\\t",'\n':"\\n",'\f':"\\f",'\r':"\\r",'"':"\\\"",'\\':"\\\\"},j;function q(a){p.lastIndex=0;return p.test(a)?"\""+a.replace(p,function(c){var f=r[c];return typeof f==="string"?f:"\\u"+("0000"+c.charCodeAt(0).toString(16)).slice(-4)})+"\"":"\""+a+"\""}function n(c,f){var a,e,d,i,k=h,g,b=f[c];if(b&&typeof b==="object"&&typeof b.toJSON==="function"){b=b.toJSON(c)}if(typeof j==="function"){b=j.call(f,c,b)}switch(typeof b){case"string":return q(b);case"number":return isFinite(b)?String(b):"null";case"boolean":case"null":return String(b);case"object":if(!b){return"null"}h+=m;g=[];if(Object.prototype.toString.apply(b)==="[object Array]"){i=b.length;for(a=0;a<i;a+=1){g[a]=n(a,b)||"null"}d=g.length===0?"[]":h?"[\n"+h+g.join(",\n"+h)+"\n"+k+"]":"["+g.join(",")+"]";h=k;return d}if(j&&typeof j==="object"){i=j.length;for(a=0;a<i;a+=1){e=j[a];if(typeof e==="string"){d=n(e,b);if(d){g.push(q(e)+(h?": ":":")+d)}}}}else{for(e in b){if(Object.hasOwnProperty.call(b,e)){d=n(e,b);if(d){g.push(q(e)+(h?": ":":")+d)}}}}d=g.length===0?"{}":h?"{\n"+h+g.join(",\n"+h)+"\n"+k+"}":"{"+g.join(",")+"}";h=k;return d;default:}}if(typeof JSON.stringify!=="function"){JSON.stringify=function(c,f,a){var e;h="";m="";if(typeof a==="number"){for(e=0;e<a;e+=1){m+=" "}}else if(typeof a==="string"){m=a}j=f;if(f&&typeof f!=="function"&&(typeof f!=="object"||typeof f.length!=="number")){throw new Error("JSON.stringify");}return n("",{'':c})}}if(typeof JSON.parse!=="function"){JSON.parse=function(i,k){var g;function b(c,f){var a,e,d=c[f];if(d&&typeof d==="object"){for(a in d){if(Object.hasOwnProperty.call(d,a)){e=b(d,a);if(e!==undefined){d[a]=e}else{delete d[a]}}}}return k.call(c,f,d)}o.lastIndex=0;if(o.test(i)){i=i.replace(o,function(c){return"\\u"+("0000"+c.charCodeAt(0).toString(16)).slice(-4)})}if(/^[\],:{}\s]*$/.test(i.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,"@").replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,"]").replace(/(?:^|:|,)(?:\s*\[)+/g,""))){g=eval("("+i+")");return typeof k==="function"?b({'':g},""):g}throw new SyntaxError("JSON.parse");}}}());



var o={a:1,b:false,c:null, d:[1,2,"hello world", true, new Date()]};
alert(JSON.stringify(o))


tested in ie7+8.

shows:
{"a":1,"b":false,"c":null,"d":[1,2,"hello world",true,"2010-04-15T23:38:24.146Z"]}

TinyScript
04-16-2010, 02:52 AM
Hmmm, thanks. I must have been doing something wrong. Imagine that? LOL

So, that toSourceCode function is useless? thanks for saving me any further wasted time.

TinyScript
04-16-2010, 07:43 AM
Hey, the JSON object didn't work in IE until I added your function, so I wasn't doing anything wrong after all. It was IE not having the JSON, am I right?

Thank you again for the JSON function. works perfect.