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>
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>