TypeError没有将Array隐式转换为String

时间:2016-04-08 08:36:18

标签: ruby json

以下说明as per these suggestions,已尝试

instructions = JSON.load(self.signature).map { |h| "line #{h['mx']},#{h['my']} #{h['lx']},#{h['ly']}" } * ' '

instructions = JSON.parse(self.signature).map { |h| "line #{h['mx'].to_i},#{h['my'].to_i} #{h['lx'].to_i},#{h['ly'].to_i}" } * ' '

生成错误no implicit conversion of Array into String

如果我在控制台中说明

as = '[{"lx":21,"ly":39,"mx":21,"my":38},{"lx":21,"ly":39,"mx":21,"my":39},{"lx":21,"ly":37,"mx":21,"my":"my":37},{"lx":21,"ly":35,"mx":21,"my":36},{"lx":22,"ly":35,"mx":21,"my":35},{"lx":23,"ly":35,"mx":22,"my":35},{"lx":28,"ly":35,"mx":23,"my":35},{"lx":32,"ly":35,"mx":28,"my":35},{"lx":37,"ly":36,"mx":32,"my":35},{"lx":42,"ly":40,"mx":37,"my":36},{"lx":46,"ly":44,"mx":42,"my":40},{"lx":49,"ly":49,"mx":46,"my":44},{"lx":51,"ly":51,"mx":49,"my":49},{"lx":52,"ly":53,"mx":51,"my":51},{"lx":53,"ly":54,"mx":52,"my":53},{"lx":54,"ly":54,"mx":53,"my":54},{"lx":55,"ly":54,"mx":54,"my":54},{"lx":58,"ly":48,"mx":55,"my":54},{"lx":62,"ly":39,"mx":58,"my":48},{"lx":68,"ly":26,"mx":62,"my":39},{"lx":71,"ly":19,"mx":68,"my":26},{"lx":74,"ly":13,"mx":71,"my":19},{"lx":76,"ly":10,"mx":74,"my":13},{"lx":77,"ly":10,"mx":76,"my":10},{"lx":78,"ly":10,"mx":77,"my":10},{"lx":82,"ly":17,"mx":78,"my":10},{"lx":86,"ly":26,"mx":82,"my":17},{"lx":89,"ly":33,"mx":86,"my":26},{"lx":92,"ly":40,"mx":89,"my":33},{"lx":95,"ly":46,"mx":92,"my":40},{"lx":97,"ly":49,"mx":95,"my":46},{"lx":98,"ly":49,"mx":97,"my":49},{"lx":99,"ly":49,"mx":98,"my":49},{"lx":101,"ly":47,"mx":99,"my":49},{"lx":103,"ly":42,"mx":101,"my":47},{"lx":106,"ly":38,"mx":103,"my":42},{"lx":108,"ly":35,"mx":106,"my":38},{"lx":109,"ly":32,"mx":108,"my":35},{"lx":110,"ly":31,"mx":109,"my":32},{"lx":111,"ly":31,"mx":110,"my":31},{"lx":114,"ly":31,"mx":111,"my":31},{"lx":117,"ly":34,"mx":114,"my":31},{"lx":120,"ly":37,"mx":117,"my":34},{"lx":123,"ly":40,"mx":120,"my":37},{"lx":126,"ly":42,"mx":123,"my":40},{"lx":128,"ly":43,"mx":126,"my":42},{"lx":130,"ly":43,"mx":128,"my":43},{"lx":131,"ly":43,"mx":130,"my":43},{"lx":132,"ly":43,"mx":131,"my":43},{"lx":134,"ly":41,"mx":132,"my":43},{"lx":134,"ly":40,"mx":134,"my":41},{"lx":135,"ly":40,"mx":134,"my":40},{"lx":136,"ly":40,"mx":135,"my":40},{"lx":139,"ly":40,"mx":136,"my":40},{"lx":142,"ly":40,"mx":139,"my":40},{"lx":146,"ly":42,"mx":142,"my":40},{"lx":152,"ly":45,"mx":146,"my":42},{"lx":156,"ly":45,"mx":152,"my":45},{"lx":159,"ly":46,"mx":156,"my":45},{"lx":162,"ly":46,"mx":159,"my":46},{"lx":164,"ly":46,"mx":162,"my":46},{"lx":165,"ly":46,"mx":164,"my":46},{"lx":166,"ly":46,"mx":165,"my":46},{"lx":167,"ly":46,"mx":166,"my":46},{"lx":167,"ly":45,"mx":167,"my":46},{"lx":167,"ly":44,"mx":167,"my":45},{"lx":167,"ly":43,"mx":167,"my":44},{"lx":167,"ly":41,"mx":167,"my":43},{"lx":167,"ly":39,"mx":167,"my":41},{"lx":167,"ly":38,"mx":167,"my":39},{"lx":167,"ly":37,"mx":167,"my":38},{"lx":168,"ly":38,"mx":167,"my":37},{"lx":169,"ly":40,"mx":168,"my":38},{"lx":170,"ly":40,"mx":169,"my":40}]'

我得到了同样的错误。这是如何得到改善的?

修改 放置self.signature的控制台输出:

   (0.1ms)  BEGIN
{"lx"=>37, "ly"=>141, "mx"=>37, "my"=>140}
{"lx"=>37, "ly"=>139, "mx"=>37, "my"=>141}
{"lx"=>41, "ly"=>121, "mx"=>37, "my"=>139}
{"lx"=>52, "ly"=>96, "mx"=>41, "my"=>121}
{"lx"=>70, "ly"=>66, "mx"=>52, "my"=>96}
{"lx"=>85, "ly"=>49, "mx"=>70, "my"=>66}
{"lx"=>95, "ly"=>39, "mx"=>85, "my"=>49}
{"lx"=>107, "ly"=>34, "mx"=>95, "my"=>39}
{"lx"=>112, "ly"=>34, "mx"=>107, "my"=>34}
{"lx"=>118, "ly"=>34, "mx"=>112, "my"=>34}
{"lx"=>122, "ly"=>39, "mx"=>118, "my"=>34}
{"lx"=>125, "ly"=>52, "mx"=>122, "my"=>39}
{"lx"=>130, "ly"=>71, "mx"=>125, "my"=>52}
{"lx"=>134, "ly"=>88, "mx"=>130, "my"=>71}
{"lx"=>140, "ly"=>101, "mx"=>134, "my"=>88}
{"lx"=>145, "ly"=>115, "mx"=>140, "my"=>101}
{"lx"=>147, "ly"=>120, "mx"=>145, "my"=>115}
{"lx"=>150, "ly"=>124, "mx"=>147, "my"=>120}
{"lx"=>151, "ly"=>125, "mx"=>150, "my"=>124}
{"lx"=>152, "ly"=>109, "mx"=>151, "my"=>125}
{"lx"=>157, "ly"=>86, "mx"=>152, "my"=>109}
{"lx"=>166, "ly"=>63, "mx"=>157, "my"=>86}
{"lx"=>174, "ly"=>51, "mx"=>166, "my"=>63}
{"lx"=>191, "ly"=>49, "mx"=>174, "my"=>51}
{"lx"=>227, "ly"=>79, "mx"=>191, "my"=>49}
{"lx"=>249, "ly"=>108, "mx"=>227, "my"=>79}
{"lx"=>275, "ly"=>140, "mx"=>249, "my"=>108}
{"lx"=>295, "ly"=>158, "mx"=>275, "my"=>140}
{"lx"=>312, "ly"=>168, "mx"=>295, "my"=>158}
{"lx"=>330, "ly"=>170, "mx"=>312, "my"=>168}
{"lx"=>336, "ly"=>150, "mx"=>330, "my"=>170}
{"lx"=>339, "ly"=>122, "mx"=>336, "my"=>150}
{"lx"=>339, "ly"=>109, "mx"=>339, "my"=>122}
{"lx"=>339, "ly"=>98, "mx"=>339, "my"=>109}
{"lx"=>339, "ly"=>92, "mx"=>339, "my"=>98}
{"lx"=>339, "ly"=>91, "mx"=>339, "my"=>92}
{"lx"=>341, "ly"=>91, "mx"=>339, "my"=>91}
{"lx"=>347, "ly"=>101, "mx"=>341, "my"=>91}
{"lx"=>353, "ly"=>110, "mx"=>347, "my"=>101}
{"lx"=>358, "ly"=>118, "mx"=>353, "my"=>110}
{"lx"=>361, "ly"=>126, "mx"=>358, "my"=>118}
{"lx"=>363, "ly"=>129, "mx"=>361, "my"=>126}
{"lx"=>363, "ly"=>130, "mx"=>363, "my"=>129}
   (0.2ms)  ROLLBACK
Completed 500 Internal Server Error in 16ms (ActiveRecord: 1.2ms)

1 个答案:

答案 0 :(得分:1)

as变量

中存在语法错误
as = '[{"lx":21,"ly":39,"mx":21,"my":38},{"lx":21,"ly":39,"mx":21,"my":39},
{"lx":21,"ly":37,"mx":21,"my":"my":37}


"my":"my":37

修改

你可以试试这个

self.signature.map { |h| "line #{h['mx'].to_i},#{h['my'].to_i} #{h['lx'].to_i},#{h['ly'].to_i}" } * ' '