Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Ruby中解析Json文件时出现问题_Json_Ruby_Discogs Api - Fatal编程技术网

在Ruby中解析Json文件时出现问题

在Ruby中解析Json文件时出现问题,json,ruby,discogs-api,Json,Ruby,Discogs Api,我对编程ruby和使用json软件包相当陌生,我想我面临着一个典型的noob错误,但我现在已经两天找不到我的错误了。。我是这样开始的: require 'discogs-wrapper' require 'json' aw = Discogs::Wrapper.new("my_application", user_token: "my_user_token") inv = aw.get_user_inventory('my_user_name', :per_page => 1, :pag

我对编程ruby和使用json软件包相当陌生,我想我面临着一个典型的noob错误,但我现在已经两天找不到我的错误了。。我是这样开始的:

require 'discogs-wrapper'
require 'json'

aw = Discogs::Wrapper.new("my_application", user_token: "my_user_token")
inv = aw.get_user_inventory('my_user_name', :per_page => 1, :page => 1)
p=JSON.parse(inv)
  {"pagination"=>
  {"per_page"=>1,
   "items"=>13692,
   "page"=>1,
   "urls"=>
    {"last"=>
      "URL containing tokens",
     "next"=>
      "URL containing tokens"},
   "pages"=>13692},
 "listings"=>
  [{"status"=>"Draft",
    "original_price"=>
     {"curr_abbr"=>"EUR",
      "formatted"=>"\u20AC10.46",
      "value"=>10.46,
      "curr_id"=>3},
    "weight"=>230.0,
    "original_shipping_price"=>
     {"curr_abbr"=>"EUR",
      "formatted"=>"\u20AC5.90",
      "value"=>5.9,
      "curr_id"=>3},
    "price"=>{"currency"=>"EUR", "value"=>10.46},
    "allow_offers"=>true,
    "uri"=>"https://www.discogs.com/sell/item/437965910",
    "sleeve_condition"=>"Very Good Plus (VG+)",
    "format_quantity"=>1,
    "id"=>437965910,
    "shipping_price"=>{"currency"=>"EUR", "value"=>5.9},
    "posted"=>"2017-02-07T23:43:01-08:00",
    "ships_from"=>"Germany",
    "in_cart"=>false,
    "comments"=>"",
    "seller"=>
     {"username"=>"zweischeiben.de",
      "stats"=>{"rating"=>"100.0", "total"=>143, "stars"=>5.0},
      "uid"=>3359767,
      "url"=>"https://api.discogs.com/users/zweischeiben.de",
      "html_url"=>"https://www.discogs.com/user/zweischeiben.de",
      "shipping"=>
       "(Lots of information about shipping, cut just cut it out)"
      "payment"=>"Bank Transfer, PayPal",
      "avatar_url"=>
       "(cut out url)",
      "resource_url"=>"https://api.discogs.com/users/zweischeiben.de",
      "id"=>3359767},
    "condition"=>"Near Mint (NM or M-)",
    "release"=>
     {"thumbnail"=>
       "(cut out url)",
      "description"=>"Steamhammer - Mountains (LP, Album, RE)",
      "artist"=>"Steamhammer",
      "format"=>"LP, Album, RE",
      "resource_url"=>"https://api.discogs.com/releases/7303333",
      "title"=>"Mountains",
      "year"=>0,
      "id"=>7303333,
      "catalog_number"=>"201.006, 0201.006"},
                        "resource_url"=>"https://api.discogs.com/marketplace/listings/437965910",
"audio"=>false,
"external_id"=>"3, 16",
"location"=>"T52574"}]}
我得到的是:

C:/Ruby24-x64/lib/ruby/2.4.0/json/common.rb:156:in `initialize': no implicit conversion of Hashie::Mash into String (TypeError)
from C:/Ruby24-x64/lib/ruby/2.4.0/json/common.rb:156:in `new'
from C:/Ruby24-x64/lib/ruby/2.4.0/json/common.rb:156:in `parse'
from C:/Users/rtuz2th/.RubyMine2017.3/config/scratches/scratch.rb:6:in `<top (required)>'
from -e:1:in `load'
from -e:1:in `<main>'
所以至少,我的代码现在运行时没有任何错误。但当我试图引用inv中的任何信息时,我只得到了空的响应。inv(不带.to_json)如下所示:

require 'discogs-wrapper'
require 'json'

aw = Discogs::Wrapper.new("my_application", user_token: "my_user_token")
inv = aw.get_user_inventory('my_user_name', :per_page => 1, :page => 1)
p=JSON.parse(inv)
  {"pagination"=>
  {"per_page"=>1,
   "items"=>13692,
   "page"=>1,
   "urls"=>
    {"last"=>
      "URL containing tokens",
     "next"=>
      "URL containing tokens"},
   "pages"=>13692},
 "listings"=>
  [{"status"=>"Draft",
    "original_price"=>
     {"curr_abbr"=>"EUR",
      "formatted"=>"\u20AC10.46",
      "value"=>10.46,
      "curr_id"=>3},
    "weight"=>230.0,
    "original_shipping_price"=>
     {"curr_abbr"=>"EUR",
      "formatted"=>"\u20AC5.90",
      "value"=>5.9,
      "curr_id"=>3},
    "price"=>{"currency"=>"EUR", "value"=>10.46},
    "allow_offers"=>true,
    "uri"=>"https://www.discogs.com/sell/item/437965910",
    "sleeve_condition"=>"Very Good Plus (VG+)",
    "format_quantity"=>1,
    "id"=>437965910,
    "shipping_price"=>{"currency"=>"EUR", "value"=>5.9},
    "posted"=>"2017-02-07T23:43:01-08:00",
    "ships_from"=>"Germany",
    "in_cart"=>false,
    "comments"=>"",
    "seller"=>
     {"username"=>"zweischeiben.de",
      "stats"=>{"rating"=>"100.0", "total"=>143, "stars"=>5.0},
      "uid"=>3359767,
      "url"=>"https://api.discogs.com/users/zweischeiben.de",
      "html_url"=>"https://www.discogs.com/user/zweischeiben.de",
      "shipping"=>
       "(Lots of information about shipping, cut just cut it out)"
      "payment"=>"Bank Transfer, PayPal",
      "avatar_url"=>
       "(cut out url)",
      "resource_url"=>"https://api.discogs.com/users/zweischeiben.de",
      "id"=>3359767},
    "condition"=>"Near Mint (NM or M-)",
    "release"=>
     {"thumbnail"=>
       "(cut out url)",
      "description"=>"Steamhammer - Mountains (LP, Album, RE)",
      "artist"=>"Steamhammer",
      "format"=>"LP, Album, RE",
      "resource_url"=>"https://api.discogs.com/releases/7303333",
      "title"=>"Mountains",
      "year"=>0,
      "id"=>7303333,
      "catalog_number"=>"201.006, 0201.006"},
                        "resource_url"=>"https://api.discogs.com/marketplace/listings/437965910",
"audio"=>false,
"external_id"=>"3, 16",
"location"=>"T52574"}]}
但如果我现在尝试参考位置:

require 'discogs-wrapper'
require 'json'

aw = Discogs::Wrapper.new("my_application", user_token: "my_token")
inv=aw.get_user_inventory('my_username', :per_page => 1, :page => 1)
inv=inv.to_json
p=JSON.parse(inv)
puts p[location]
我只是犯了一个这样的错误

C:\Ruby24-x64\bin\ruby.exe -e     $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift)             C:/Users/rtuz2th/.RubyMine2017.3/config/scratches/scratch.rb
C:/Users/rtuz2th/.RubyMine2017.3/config/scratches/scratch.rb:9:in `<top (required)>': undefined local variable or method `location' for main:Object (NameError)
from -e:1:in `load'
from -e:1:in `<main>'

Process finished with exit code 1
C:\Ruby24-x64\bin\ruby.exe-e$stdout.sync=true;$stderr.sync=true;加载($0=ARGV.shift)C:/Users/rtuz2t/.RubyMine2017.3/config/scratch/scratch.rb
C:/Users/rtuz2th/.RubyMine2017.3/config/scratches/scratch.rb:9:in``中:未定义的局部变量或main:Object的方法'location'(NameError)
from-e:1:in“load”
from-e:1:in`'
进程已完成,退出代码为1
或者是一个空洞的回答。我两天来一直在努力解决这个问题,但是我完全没有主意了。提前谢谢你的帮助

inv = inv.to_json
p = JSON.parse(inv)
上面是一个noop。按原样使用
inv
,它已经是一个散列了


您不能只查找位于树层次结构中较深位置的任何键或进行哈希。哈希有stings作为键,它有两个顶级元素,
'pagination'
'listings'
,后者是一个数组,此数组的每个元素都是具有
location
键的哈希



在剪切长值时,删除了结尾的逗号,使我无法复制粘贴输入并提供准确答案。

put p[location]
正在寻找一个名为
location
的变量。您是指
'location'
还是
:location
?但是,回到您最初的实现,
aw.get\u user\u inventory
似乎已经返回了一个哈希类型的对象,而不是JSON,所以您不需要
JSON.parse
它返回什么。当我try-puts p['location']我只是得到一个空的响应。对不起,我对这整件事很陌生,我不明白你的第二句话。我试图引用inv而不分析它,但我从中什么也得不到。只是尝试了pp-inv['location']而根本不分析它,只是得到了一个零作为响应。