0
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"

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

4

2 回答 2

1

使用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"

你到底得到了什么错误?

于 2013-04-23T20:44:32.590 回答
1

反斜杠只是转义引号;它们实际上不在字符串中。至于eval解析它,如果它来自受信任的来源,请使用并让 Ruby 解析它。

于 2013-04-23T20:39:16.760 回答