我正在处理一个可以像这样吐出 JSON 的 API:
{
"artist_name": "Wilhelm Stenhammar",
"artist_name:1": "Helsingborg Symphony Orchestra",
"artist_name:10": "NDR Radiophilharmonie",
"artist_name:11": "Malm\u00f6 Opera Orchestra",
"artist_name:12": "Radio Filharmonisch Orkest Holland",
"artist_name:13": "Symphony Orchestra Of Norrlands Opera",
"artist_name:14": "Pilsen Philharmonic Orchestra",
"artist_name:15": "Latvian National Symphony",
"artist_name:16": "SWR Radiofunkorchester, Kaiserslauten",
"artist_name:17": "Orquesta Sinf\u00f3nica de San Luis Potos\u00ed",
"artist_name:2": "Arvo Volmer",
"artist_name:3": "V\u00e4ster\u00e5s Symphony Orchestra",
"artist_name:4": "G\u00e4vle Symphony Orchestra",
"artist_name:5": "Royal Danish Orchestra",
"artist_name:6": "Stuttgart Philharmonic Orchestra",
"artist_name:7": "Turku Philharmonic Orchestra",
"artist_name:8": "Gothenburg Opera Orchestra",
"artist_name:9": "Moscow Symphony Orchestra",
"artist_uri": "spotify:artist:6gfRaesyLupPd0gDQaLwJV",
"artist_uri:1": "spotify:artist:0K2m2TlgHkcp32NAZV6Omw",
"artist_uri:10": "spotify:artist:6i9KFEJuHq78nMsQxeB1vR",
"artist_uri:11": "spotify:artist:2bgesrziKLjYqObZ1i6KpO",
"artist_uri:12": "spotify:artist:4TlqQbYkCmCDsrlkI2a09s",
"artist_uri:13": "spotify:artist:1sKkv2iobrvnJtPtLJABf4",
"artist_uri:14": "spotify:artist:5DEoOcExTp3y8yRzBVG1Bw",
"artist_uri:15": "spotify:artist:4WP9O07oZuFjNpaoo8ktCf",
"artist_uri:16": "spotify:artist:0AgZxnsoQq8uKYQEDQnO9N",
"artist_uri:17": "spotify:artist:5qXInUZHHgmah0TPvUuq4l",
"artist_uri:2": "spotify:artist:5zZjojCwIPWfzXOtSKTa0W",
"artist_uri:3": "spotify:artist:4OYpD0LcwEZb5BdhyeJ7zT",
"artist_uri:4": "spotify:artist:2DMF5PvArh8PhpR1YRDBgv",
"artist_uri:5": "spotify:artist:3JZPA9VxDLixJfKDNbmlbT",
"artist_uri:6": "spotify:artist:2G3SGwAd7uOTVm1y4IijBg",
"artist_uri:7": "spotify:artist:2yJX6ev7Io4KagCeNdXFBB",
"artist_uri:8": "spotify:artist:6fqElxCA09nHlqdkzY1sN9",
"artist_uri:9": "spotify:artist:0udLXDzunp4xQRWhMNQPci", }
}
该对象有多个艺术家姓名和每个艺术家的 URI。在某些情况下,艺术家的数量可能多于或少于此处的示例,包括只有 1 名的最常见情况。
{
"artist_name": "Wilhelm Stenhammar",
"artist_uri": "spotify:artist:6gfRaesyLupPd0gDQaLwJV",
}
解除此对象以捕获所有可能数量的艺术家姓名和 URI 的最佳方法是什么?最后,我想要一个包含 Name 和 URI 字段的 Artist 对象数组,unmartialed 数组应该反射(reflect) JSON 中艺术家的顺序。
最佳答案
您可以做的最简单的事情是将其解码为 map[string]string 并按原样使用它,但您可以做一件更简洁的事情:定义
type Artist struct {
Name string
URI string // could be *uri.URL instead
}
type Foo struct {
Artists []Artist
}
然后写一个 func (f *Foo) UnmarshalJSON(data []byte) error { 解码成一个 map[string]string,然后检查每个一对 key 依次。如果找到它们,它会附加到 f.Artists,如果找不到,它就会停止。
关于json - 取消具有可变数量的相似字段的 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48777392/
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev
我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..
我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss
我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢
好的,所以我的目标是轻松地将一些数据保存到磁盘以备后用。您如何简单地写入然后读取一个对象?所以如果我有一个简单的类classCattr_accessor:a,:bdefinitialize(a,b)@a,@b=a,bendend所以如果我从中非常快地制作一个objobj=C.new("foo","bar")#justgaveitsomerandomvalues然后我可以把它变成一个kindaidstring=obj.to_s#whichreturns""我终于可以将此字符串打印到文件或其他内容中。我的问题是,我该如何再次将这个id变回一个对象?我知道我可以自己挑选信息并制作一个接受该信
在我的Controller中,我通过以下方式在我的index方法中支持HTML和JSON:respond_todo|format|format.htmlformat.json{renderjson:@user}end在浏览器中拉起它时,它会自然地以HTML呈现。但是,当我对/user资源进行内容类型为application/json的curl调用时(因为它是索引方法),我仍然将HTML作为响应。如何获取JSON作为响应?我还需要说明什么? 最佳答案 您应该将.json附加到请求的url,提供的格式在routes.rb的路径中定义。这
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象
我在Rails工作并有以下类(class):classPlayer当我运行时bundleexecrailsconsole然后尝试:a=Player.new("me",5.0,"UCLA")我回来了:=>#我不知道为什么Player对象不会在这里初始化。关于可能导致此问题的操作/解释的任何建议?谢谢,马里奥格 最佳答案 havenoideawhythePlayerobjectwouldn'tbeinitializedhere它没有初始化很简单,因为你还没有初始化它!您已经覆盖了ActiveRecord::Base初始化方法,但您没有调
我有一个服务模型/表及其注册表。在表单中,我几乎拥有服务的所有字段,但我想在验证服务对象之前自动设置其中一些值。示例:--服务Controller#创建Action:defcreate@service=Service.new@service_form=ServiceFormObject.new(@service)@service_form.validate(params[:service_form_object])and@service_form.saverespond_with(@service_form,location:admin_services_path)end在验证@ser