我有一个Cloud Code脚本,可以从服务中提取一些JSON。该JSON包含一组对象。我想将它们保存到Parse,但使用特定的Parse类。我该怎么办?
这是我的代码。
Parse.Cloud.httpRequest({
url: 'http://myservicehost.com',
headers: {
'Authorization': 'XXX'
},
success: function(httpResponse) {
console.log("Success!");
var json = JSON.parse(httpResponse.text);
var recipes = json.results;
for(int i=0; i<recipes.length; i++) {
var Recipe = Parse.Object.extend("Recipe");
var recipeFromJSON = recipes[i];
// how do i save recipeFromJSON into Recipe without setting all the fields one by one?
}
}
});
答案 0 :(得分:5)
我想我已经开始工作了。您需要将JSON数据对象中的className属性设置为您的类名。 (在source code中找到它)但我只在客户端尝试过这个。
for(int i=0; i<recipes.length; i++) {
var recipeFromJSON = recipes[i];
recipeFromJSON.className = "Recipe";
var recipeParseObject = Parse.Object.fromJSON(recipeFromJSON);
// do stuff with recipeParseObject
}
答案 1 :(得分:1)
此页面的示例https://parse.com/docs/js/guide
var GameScore = Parse.Object.extend("GameScore");
var gameScore = new GameScore();
gameScore.save({
score: 1337,
playerName: "Sean Plott",
cheatMode: false
}, {
success: function(gameScore) {
// The object was saved successfully.
},
error: function(gameScore, error) {
// The save failed.
// error is a Parse.Error with an error code and message.
}
});
答案 2 :(得分:1)
IHMO这个问题不是How to use Parse.Object fromJSON? [duplicate]的重复
在此问题中,JSON不是由Parse.Object.toJSON函数本身生成的,而是来自其他服务的。
const object = new Parse.Object('MyClass')
const asJson = object.toJSON();
// asJson.className = 'MyClass';
Parse.Object.fromJSON(asJson);
// Without L3 this results into:
// Error: Cannot create an object without a className
// It makes no sense (to me) why the Parse.Object.toJSON is not reversible