JavaScript Object JSON

var myJSONtext = '{ "name": "Violet", "occupation": "character" }';   
var myJSONobj = JSON.parse(myJSONtext, function reviver(key, value) {
	var type;
	if (value && typeof value === 'object') {
		type = value.type;
		if (typeof type === 'string' && typeof window[type] === 'function') {
			return new (window[type])(value);
		}
	}
	return value;
}); 
alert('Json>Obj:'+myJSONobj.name);    

alert('Obj>Json:'+JSON.stringify(myJSONobj, function replacer(key, value) {
	var type;
	if (value && typeof value === 'object') {
		type = value.type;
		if (typeof type === 'string' && typeof window[type] === 'function') {
			return new (window[type])(value);
		}
	}
	return value;
}));

 

你可能感兴趣的:(JavaScript,json)