这个问题在这里已经有了答案:
在Node.js中反序列化后,将对象与其类重新关联 2个答案我有一个问题来反序列化JSON文本到一个javascript对象,我测试jquery和yui库,我有这个类:
function Identifier(name, contextId) { this.name = name; this.contextId = contextId; } Identifier.prototype.setName = function(name) { this.name = name; } Identifier.prototype.getName = function() { return this.name; } Identifier.prototype.setContextId = function(contexId) { this.contextId= contexId; } Identifier.prototype.getContextId = function() { return this.contextId; }我有这个JSON:
{ "Identifier": { "name":"uno", "contextId":"dos"} }我想解析创建一个Identifier对象,我的问题是这样的句子:
var obj = jQuery.parseJSON('{"Identifier": { "name":"uno","contextId":"dos"}}');要么
var obj2 = JSON.parse('{"Identifier": { "name":"uno","contextId":"dos"}}');不工作,var obj和obj2不是标识符对象,我怎么解析这个? 谢谢
这个问题不是重复的,因为它比迈克尔标记为重复的问题早5年
This question already has an answer here:
Re-associating an object with its class after deserialization in Node.js 2 answersi have a question to deserialize JSON text to an javascript object, i test jquery and yui library, i have this class:
function Identifier(name, contextId) { this.name = name; this.contextId = contextId; } Identifier.prototype.setName = function(name) { this.name = name; } Identifier.prototype.getName = function() { return this.name; } Identifier.prototype.setContextId = function(contexId) { this.contextId= contexId; } Identifier.prototype.getContextId = function() { return this.contextId; }and i have this JSON:
{ "Identifier": { "name":"uno", "contextId":"dos"} }I want to the parse create an Identifier object, my problem is that this sentences:
var obj = jQuery.parseJSON('{"Identifier": { "name":"uno","contextId":"dos"}}');or
var obj2 = JSON.parse('{"Identifier": { "name":"uno","contextId":"dos"}}');Dont work, the var obj and obj2 aren't an Identifier object, how can i parse this? Thanks
This question is not the duplicate, because it was made 5 years before than the question that Michael marks as duplicated
最满意答案
你可以创建一个函数为你初始化这些对象。 这是我迅速起草的一个:
function parseJSONToObject(str) { var json = JSON.parse(str); var name = null; for(var i in json) { //Get the first property to act as name name = i; break; } if (name == null) return null; var obj = new window[name](); for(var i in json[name]) obj[i] = json[name][i]; return obj; }这将创建一个由第一个属性的名称表示的类型的对象,并根据第一个属性的对象的属性为其分配值。 你可以这样使用它:
var identifier = parseJSONToObject('{"Identifier": { "name":"uno","contextId":"dos"}}'); console.log(identifier);现场示例
You could create a function that initializes those objects for you. Here's one I quickly drafted:
function parseJSONToObject(str) { var json = JSON.parse(str); var name = null; for(var i in json) { //Get the first property to act as name name = i; break; } if (name == null) return null; var obj = new window[name](); for(var i in json[name]) obj[i] = json[name][i]; return obj; }This creates an object of the type represented by the name of the first attribute, and assigns it's values according to the attributes of the object of the first attribute. You could use it like that:
var identifier = parseJSONToObject('{"Identifier": { "name":"uno","contextId":"dos"}}'); console.log(identifier);Live example
更多推荐
发布评论