繁体   English   中英

Ruby:使用反斜杠解析字符串

[英]Ruby: parsing string with backslash

string = "{:name=>\"2012 Honda Civic EX-L\", :price=>\"23100\", :dealer_name=>\"T AND T HOND\", :dealer_website=>\"http://autocatch.com/dealer/t-and-t-honda/index.htm\", :phone=>\"(888) 364-1858\", :ad=>\"1167269\", :stock=>\" 2120054N\", :year=>\"2012\", :make=>\"Honda\", :model=>\"Model Civic\", :trim=>\"EXL\", :mileage=>\"4629\", :body_style=>\"Coupe\", :transmission=>\"Automatic\", :ext_colour=>\"White\", :int_colour=>\"Grey\", :doors=>\"2Door\", :passengers=>\"Passengers \", :drive_train=>\"Front Wheel Drive\", :engine=>\"1.80\", :cylinders=>\"4Cylinder\", :fuel_type=>\"Type Gas\", :certified=>\"\", :e_tested=>\"\"}\n"

我该如何解析它? 它一直给我错误。

反斜杠正在逃避引号; 它们实际上并不字符串中。 至于解析它,使用eval并让Ruby解析它,如果它来自可靠的源。

使用ruby-2.0.0-p0

string = "{:name=>\"2012 Honda Civic EX-L\", :price=>\"23100\", :dealer_name=>\"T AND T HOND\", :dealer_website=>\"http://autocatch.com/dealer/t-and-t-honda/index.htm\", :phone=>\"(888) 364-1858\", :ad=>\"1167269\", :stock=>\" 2120054N\", :year=>\"2012\", :make=>\"Honda\", :model=>\"Model Civic\", :trim=>\"EXL\", :mileage=>\"4629\", :body_style=>\"Coupe\", :transmission=>\"Automatic\", :ext_colour=>\"White\", :int_colour=>\"Grey\", :doors=>\"2Door\", :passengers=>\"Passengers \", :drive_train=>\"Front Wheel Drive\", :engine=>\"1.80\", :cylinders=>\"4Cylinder\", :fuel_type=>\"Type Gas\", :certified=>\"\", :e_tested=>\"\"}\n"
hash = eval(string)
hash[:name] # => "2012 Honda Civic EX-L"

你得到了什么错误?

暂无
暂无

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

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