【问题标题】:Storing nested Json in parse.com classes在 parse.com 类中存储嵌套的 Json
【发布时间】:2014-07-22 21:25:07
【问题描述】:

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

{
  "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"
    }
  ]
}

【问题讨论】:

    标签: json class object parse-platform


    【解决方案1】:

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

    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
    });
    

    还有许多其他好处..

    【讨论】:

    • 好的,所以我仍然规范化我的数据结构,只是没有连接表和外键?
    • Right.. 如果您将解析对象(或解析对象数组)保存到另一个对象,则会为您处理外键/连接表概念。还有关系表的概念,文档里都有。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-03
    • 1970-01-01
    • 2011-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多