作者:灬我的独一小姐灬 | 来源:互联网 | 2023-01-13 09:33
IwasworkingontheAndroidandfoundGsonasahandyutilitytoconvertJSONobjectsintoJavaobj
I was working on the Android and found Gson as a handy utility to convert JSON objects into Java objects. Since I am a big fan of Object Oriented architecture, I am trying to optimize angular code using OO architecture. Every object is mapped to a factory. I am wondering if there is any plugin in Javascript or angular that can convert JSON to Angular Objects. e.g. If I have a card factory in Angular
我正在使用Android,并发现Gson是一个将JSON对象转换为Java对象的便捷工具。由于我是面向对象架构的忠实粉丝,我正在尝试使用OO架构优化角度代码。每个对象都映射到一个工厂。我想知道是否有任何Javascript或angular中的插件可以将JSON转换为Angular对象。例如如果我在Angular有一家卡片工厂
app.factory('Card', [function() {
function Card(cardData) {
if (cardData) {
this.setData(cardData);
}else{
this.new();
}
};
Card.prototype = {
new: function(){
var cardData = {
title: 'Add your recommendations',
}
this.setData(cardData);
}
};
return Card;
}]);
and I am getting JSON data like this
我正在获取这样的JSON数据
{card: {title: 'demo_title'}}
it should map it automatically like GSON does. I can create a new module to do that, just wondering if someone already did that.
它应该像GSON一样自动映射。我可以创建一个新模块来做到这一点,只是想知道是否有人已经这样做了。
1 个解决方案
0
You don't need to create something to work like GSON
because JSON has native support on Javascript via JSON.parse("{\"card\": {\"title\": 'demo_title'}}"
which will produce a Javascript object like {card: {title: 'demo_title'}}
. In addition, the angularjs' $http
service, parses your JSON internally, so you don't even have to think about it.
你不需要像GSON一样创建一些东西,因为JSON通过JSON.parse(“{\”card \“:{\”title \“:'demo_title'}}”对Javascript有本机支持,这将产生一个Javascript对象如{card:{title:'demo_title'}}。此外,angularjs'$ http服务在内部解析你的JSON,所以你甚至不必考虑它。
However, what I think you are looking for, is an approach for using data from the server and instantiate a class from the parsed JSON. A regular approach for that, is using a class which in its constructor you provide the raw object and merge it into its properties. It's convenient to use angular.extend()
but you can do it manually if you prefer.
但是,我认为您正在寻找的是一种使用来自服务器的数据并从解析的JSON中实例化一个类的方法。一种常规的方法是使用一个类,在它的构造函数中,您提供原始对象并将其合并到其属性中。使用angular.extend()很方便,但如果您愿意,可以手动执行。
It'd be something like this:
它是这样的:
function Card(data) {
angular.extend(this, data);
}
var myCard = new Card({ title: 'demo_title' });
Furthermore, if you want to keep a more reliable model, you can declare a class to work as the object to hold the data, and add methods to retrieve and post data to the server.
此外,如果要保持更可靠的模型,可以声明一个类作为保存数据的对象,并添加检索数据并将数据发布到服务器的方法。
angular.module('app')
.factory('Card', ['$http', function CardFactory($http) {
var Card = function (data) {
angular.extend(this, data);
}
Card.get = function (id) {
return $http.get('https://my.api/cards/' + id).then(function(response) {
return new Card(response.data.card);
});
};
Card.all = function () {
return $http.get('https://my.api/cards').then(function (res) {
return res.data.cards.map(function (x) {
return new Card(x);
});
});
};
Card.prototype.create = function () {
var card = this;
return $http.post('https://my.api/cards', card).then(function(response) {
card.id = response.data.card.id;
return card;
});
}
return Card;
}]);
So that you can use it like this:
所以你可以像这样使用它:
angular.module('app')
.controller('CardCtrl', ['Card', function CardsCtrl(Card) {
// get a card
this.card = Card.get(1);
// create a new card
this.newCard = function newCard() {
var card = new Card({ title: 'new_card' });
card.create();
};
});
Ref.: Recommended way of getting data from the server
参考:从服务器获取数据的推荐方法