将JSON反序列化为JAVASCRIPT对象[复制](deserialize JSON to JAVASCRIPT object [duplicate])

编程入门 行业动态 更新时间:2024-10-13 16:22:32
将JSON反序列化为JAVASCRIPT对象[复制](deserialize JSON to JAVASCRIPT object [duplicate])

这个问题在这里已经有了答案:

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

i 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

更多推荐

本文发布于:2023-08-04 23:16:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1423414.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:序列   对象   JAVASCRIPT   JSON   object

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!