简体   繁体   English

航行如何将数组中的字符串类型转换为json

[英]Sails how to transform string type in array to json

I have a simple REST application built in Sails and I want to save the correct data in a model attribute of type array . 我有一个用Sails内置的简单REST应用程序,我想将正确的数据保存在array类型的模型属性中。

Route to post 张贴路线

http://localhost:1337/locations/create?name=savassibeer&locations={latitude:23789472398.2344,longitude:2734637892.56756756}&locations={latitude:22.2344,longitude:2562.56756756,date:2014-02-15T11:00:00}

The result 结果

{
  name: "savassibeer",
  locations: [
    "{latitude:23789472398.2344,longitude:2734637892.56756756}",
    "{latitude:22.2344,longitude:2562.56756756,date:2014-02-15T21:49:23.084Z}"
  ],
  createdAt: "2014-02-15T21:49:23.084Z",
  updatedAt: "2014-02-15T21:49:23.084Z",
  id: "52ffe0e345d19ec72b4fac77"
}

How can I transform the strings in locations to a valid JSON Object and save it? 如何将locations的字符串转换为有效的JSON对象并保存?

You're not going to be able to do this using URL shortcuts (ie hitting /locations/create in the browser). 您将无法使用URL快捷方式(即在浏览器中单击/locations/create )来执行此操作。 They're not designed to do type-guessing. 它们并非旨在进行类型猜测。 If you want to save data this way, the answer is to write a custom create action in your Locations controller that will 1) validate the locations data as @marionebl mentions above, and 2) set the locations attribute as a Javascript array. 如果您想以这种方式保存数据,答案是在Locations控制器中编写一个自定义的create动作,该动作将1)验证位置数据(如上文@marionebl所述),以及2)将locations属性设置为Javascript数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM