在parse.com类中存储嵌套的Json

时间:2014-07-22 21:25:07

标签: json class object parse-platform

我为我的第一个parse.com应用设置了数据结构。我来自关系数据背景,所以我不确定在解析中存储数据时我能做什么,不能做什么。 所以我的问题是,我可以将下面的Json存储在一个解析类中作为一个对象,还是我必须将它拆分为多个类/对象以用于" fixtures"和"位置"字段?

{
  "name": "fast five",
  "rules": "round robin",
  "location": [
  {
    "long":"18.633456",
    "lat":"-33.880178", 
    "venue":"Velodrome, Cape Town", 
    "date_time":"2011-08-21T18:02:52.249Z"
    }
  ],
  "teams": [
    "gauteng west",
    "gauteng north"
  ],
  "fixtures": [
  {
    "teamA":"gauteng west",
    "teamB":"gauteng west", 
    "court":"court 5", 
    "date_time":"2011-08-21T18:02:52.249Z"
    },
    {
    "teamA":"gauteng west",
    "teamB":"gauteng west", 
    "court":"court 5", 
    "date_time":"2011-08-21T18:02:52.249Z"
    }
  ]
}

1 个答案:

答案 0 :(得分:3)

Parse支持将JSON存储在Parse对象的列中,但您无法根据其中的值进行查询。很难快速勾画出使用更多Parse数据的完美模式,但它可能是这样的:

Location class
-venue   : "Velodrome, Cape Town"
-date    : a date object 
-location: a Parse GeoPoint object with that lat/lon

Team class:
-name: "gauteng west"

Fixture class:
-teamA   : a Team class object
-teamB   : a Team class object
-location: a Location class object
-court   : "court 5"
-date    : a date object 

Event class
-name    : "fast five"
-rules   : "round robin"
-teams   : an array of Team class objects
-location: a Location class object 
-fixtures: an array of Fixture class objects

通过这种分离,您可以立即获取事件的所有数据:

var query = new Parse.Query("Event");
query.include(['teams', 'fixtures', 'location']);
query.first().then(function(event) {
  var teams = event.get('teams');
  console.log(teams[0].get('name'));
});

或查询指定地点附近的事件:

var locQuery = new Parse.Query("Location");
locQuery.near("location", a Parse GeoPoint object);
var query = new Parse.Query("Event");
query.matchesQuery("location", locQuery);
query.find().then(function(results) {
  // has all events sorted by distance from provided geopoint
}, function(err) {
  // error
});

还有许多其他好处..