4

我有一个从服务中提取一些 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?
        }
    }
});
4

3 回答 3

6

我想我成功了。您需要将 JSON 数据对象中的 className 属性设置为您的类名。(在源代码中找到它)但我只在客户端尝试过。

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
}
于 2015-12-10T14:54:24.500 回答
1

IHMO 这个问题不是How to use Parse.Object fromJSON? [复制]

在这个问题中,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 
于 2020-06-05T15:16:17.280 回答
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.
  }
});
于 2015-08-24T22:09:34.277 回答