1

在 C# 中:

var cities = clients.Select(c => 
                                new { 
                                      DisplayText = c.CityName, 
                                      Value = c.CityId 
                            });
return Json(new { Result = "OK", Options = cities });

如何在 Rails 中执行此操作?

目标json:

{
 "Result" : "OK",
 "Options": [
              { "DisplayText" : "clientName1", "Value" : 1},
              { "DisplayText" : "clientName2", "Value" : 2},
            ]
}
4

1 回答 1

4

你可以这样做:

cities = Clients.all.map { |client| {:DisplayText => client.CityName, :Value => client.CityID} }

render :json => {:Result => 'OK', :Options => cities}

请注意,大写在 ruby​​ 中并不常见。

我会做这样的事情:

cities = Clients.all.map { |client| {:displayText => client.cityName, :value => client.cityID} }

render :json => {:result => 'OK', :options => cities}
于 2013-03-05T12:40:16.030 回答