在Rails中创建JSON对象标头/结构

时间:2018-10-31 18:13:35

标签: ruby-on-rails json

我的Rails应用程序中具有以下方法,效果很好。基本上,我抓住了一堆对象(eventcaptableshareholders),然后将它们合并为一个@snapshot对象,然后将其转换为to_json并放入它在数据库中。

def save_captable_snapshot(transaction)

    @transaction = transaction
    @event = @transaction.event 
    @captable = @transaction.captable
    @shareholders = @captable.company.shareholders

    @snapshot = []
    @snapshot.push(@captable, @event, @shareholders)

    @event.update_attribute(:snapshot, @event.snapshot = @snapshot.to_json)  

  end

如果在视图中吐出@ event.snapshot,则会得到以下JSON。

[
   {
      "id":27,
      "total_stocks_in_company":"100.0",
      "version":1,
      "name":"cap table ",
      "company_id":20,
      "created_at":"2018-10-25T18:35:13.413Z",
      "updated_at":"2018-10-25T18:35:54.431Z"
   },
   {
      "status_locked":null,
      "id":45,
      "price_per_share":"100.0",
      "total_company_stocks_after_event":"100.0",
      "name":"funding",
      "date":"2018-10-25",
      "currency":"",
      "valuation":"10000.0",
      "created_at":"2018-10-25T18:35:19.273Z",
      "updated_at":"2018-10-25T18:35:54.447Z",
      "captable_id":27,
      "company_id":20,
      "snapshot":null
   },
   [
      {
         "id":0,
         "shareholder":"Bob",
         "name":"Bob",
         "number_of_stocks":null,
         "company_id":20,
         "created_at":"2018-10-25T18:33:20.770Z",
         "updated_at":"2018-10-25T18:33:20.770Z",
         "ownership_percentage":null,
         "email":"",
         "telephone":""
      },
      {
         "id":48,
         "shareholder":"Peter",
         "name":"Peter",
         "number_of_stocks":"100.0",
         "company_id":20,
         "created_at":"2018-10-25T18:33:33.109Z",
         "updated_at":"2018-10-25T18:35:54.437Z",
         "ownership_percentage":"1.0",
         "email":"",
         "telephone":""
      },
      {
         "id":49,
         "shareholder":"BobJane",
         "name":"BobJane",
         "number_of_stocks":null,
         "company_id":20,
         "created_at":"2018-10-25T18:33:45.695Z",
         "updated_at":"2018-10-25T18:33:45.695Z",
         "ownership_percentage":null,
         "email":"",
         "telephone":""
      }
   ]
]

如您所见,对象上没有格式,因此使用它很麻烦。我想要实现的是类似于以下内容(伪代码)。

{
   "captable":{
      "id":2,
      "name":"A Great Captable"
   },
   "event":{
      "id":24,
      "name":25,
      "number of stocks":3040
   },
   "shareholders":[
      {
         "id":345,
         "name":"Bob",
         "ownership percentage":0.45
      },
      {
         "id":345,
         "name":"Bob",
         "ownership percentage":0.45
      },
      {
         "id":345,
         "name":"Bob",
         "ownership percentage":0.45
      }
   ]
}

我尝试了很多不同的方法,但是无法将3个“标题”成功地放入JSON对象-3个标题或部分分别为captableevent和每个{{1} }。

如何最好地设置一个空对象,然后将3个对象压入该对象,然后再将其保存到数据库中?任何建议将不胜感激! :)

1 个答案:

答案 0 :(得分:3)

使用哈希代替数组:

class Nav extends Component {
    state = {
        background: "skyblue",
        navSize: "130px",
        activeItem: '',
        options : [
            {key: 'logout', text: 'logout'}
        ]
    }
    handleItemClick = (e) => this.setState({activeItem: e})
    componentDidMount() {
        window.addEventListener('scroll', this.handleScroll);
    } 
    componentWillUnmount() {
         window.removeEventListener('scroll', this.handleScroll);
    }