如何在子表中添加JSON数据与Ruby on Rails中的父表?

时间:2013-04-05 14:22:26

标签: ruby-on-rails ruby json

我一直在使用Ruby on Rails中的某些东西。

我有四个相互关联的表:ABCDA是[{1}}的父级,BBC的父级。

我在表D中已经存在记录,并希望针对特定记录添加多个条目,例如“3”,在“C”和“D”表中针对此ID添加。

数据格式为:

B

我从网络服务获取此数据。但我认为[{\"waypoint\":{\"latitude\":37.3645616666667,\"timestamp\":\"2012-10-16T09:58:50Z\",\"background\":false,\"estimated_speed\":17.4189262390137,\"journey_id\":null,\"longitude\":-112.850676666667}},{\"waypoint\":{\"latitude\":37.3648733333333,\"timestamp\":\"2012-10-16T09:58:54Z\",\"background\":false,\"estimated_speed\":17.076057434082,\"journey_id\":null,\"longitude\":-112.85077}},{\"waypoint\":{\"latitude\":37.3651116666667,\"timestamp\":\"2012-10-16T09:58:57Z\",\"background\":false,\"estimated_speed\":15.4269437789917,\"journey_id\":null,\"longitude\":-112.850766666667}},{\"waypoint\":{\"latitude\":37.36547,\"timestamp\":\"2012-10-16T09:59:02Z\",\"background\":false,\"estimated_speed\":17.1007328033447,\"journey_id\":null,\"longitude\":-112.85072}},{\"waypoint\":{\"latitude\":37.3658433333333,\"timestamp\":\"2012-10-16T09:59:11Z\",\"background\":false,\"estimated_speed\":10.3052024841309,\"journey_id\":null,\"longitude\":-112.850738333333}}]" 为空,而我希望它为journey_id,因为我想根据此ID进行输入。

如何使用此ID在子表中保存此数据?

1 个答案:

答案 0 :(得分:1)

您的JSON字符串未在示例中正确打开,因为它缺少前导'"'。修复并继续前进,这就是JSON看起来像“美化”的东西:

[
  {
    "waypoint": {
      "latitude": 37.3645616666667,
      "timestamp": "2012-10-16T09:58:50Z",
      "background": false,
      "estimated_speed": 17.4189262390137,
      "journey_id": null,
      "longitude": -112.850676666667
    }
  },
  {
    "waypoint": {
      "latitude": 37.3648733333333,
      "timestamp": "2012-10-16T09:58:54Z",
      "background": false,
      "estimated_speed": 17.076057434082,
      "journey_id": null,
      "longitude": -112.85077
    }
  },
  {
    "waypoint": {
      "latitude": 37.3651116666667,
      "timestamp": "2012-10-16T09:58:57Z",
      "background": false,
      "estimated_speed": 15.4269437789917,
      "journey_id": null,
      "longitude": -112.850766666667
    }
  },
  {
    "waypoint": {
      "latitude": 37.36547,
      "timestamp": "2012-10-16T09:59:02Z",
      "background": false,
      "estimated_speed": 17.1007328033447,
      "journey_id": null,
      "longitude": -112.85072
    }
  },
  {
    "waypoint": {
      "latitude": 37.3658433333333,
      "timestamp": "2012-10-16T09:59:11Z",
      "background": false,
      "estimated_speed": 10.3052024841309,
      "journey_id": null,
      "longitude": -112.850738333333
    }
  }
]

您有一组waypoint个对象。将JSON解析为Ruby对象:

obj = JSON["[{\"waypoint\":..."] # purposely truncated for brevity

返回一个哈希数组:

[{"waypoint"=>
   {"latitude"=>37.3645616666667,
    "timestamp"=>"2012-10-16T09:58:50Z",
    "background"=>false,
    "estimated_speed"=>17.4189262390137,
    "journey_id"=>nil,
    "longitude"=>-112.850676666667}},
 {"waypoint"=>
   {"latitude"=>37.3648733333333,
    "timestamp"=>"2012-10-16T09:58:54Z",
    "background"=>false,
    "estimated_speed"=>17.076057434082,
    "journey_id"=>nil,
    "longitude"=>-112.85077}},
 {"waypoint"=>
   {"latitude"=>37.3651116666667,
    "timestamp"=>"2012-10-16T09:58:57Z",
    "background"=>false,
    "estimated_speed"=>15.4269437789917,
    "journey_id"=>nil,
    "longitude"=>-112.850766666667}},
 {"waypoint"=>
   {"latitude"=>37.36547,
    "timestamp"=>"2012-10-16T09:59:02Z",
    "background"=>false,
    "estimated_speed"=>17.1007328033447,
    "journey_id"=>nil,
    "longitude"=>-112.85072}},
 {"waypoint"=>
   {"latitude"=>37.3658433333333,
    "timestamp"=>"2012-10-16T09:59:11Z",
    "background"=>false,
    "estimated_speed"=>10.3052024841309,
    "journey_id"=>nil,
    "longitude"=>-112.850738333333}}]

您可以浏览该数组并访问或更改journey_id的值:

row = 3
obj = obj.map{ |h| h['waypoint']['journey_id'] = row }
obj.first

查看第一个哈希显示值已更改,其余部分均已更改:

{
  "waypoint" => {
    "latitude" => 37.3645616666667,
    "timestamp" => "2012-10-16T09:58:50Z",
    "background" => false,
    "estimated_speed" => 17.4189262390137,
    "journey_id" => 3,
    "longitude" => -112.850676666667
  }
}

此时,您需要重新创建JSON字符串。你可以通过阅读the JSON documentation来解决这个问题。

您可以通过直接修改收到的字符串来完成所有这些操作,但您不想养成直接修改JSON字符串的习惯,因为您可能会无意中损坏有效负载。最好让解析器为您提供结构,修改它,然后让JSON重新创建字符串。

如何将它存储到数据库中也可以作为练习。