0

I have two JSON returns.

One I am able to parse fine

{"login"=>"foo", "id"=>bar,

with

@foobar_collect["login"]

But one I am having issues with

{"items"=>[{"user_id"=>foo, "user_type"=>"bar", 

I try

@foobar_collect["items"]["user_id"]

And it gives me an error no implicit conversion of String into Integer

What am I doing wrong?

1
  • Did either of the answers below solve your problem? Commented Aug 10, 2013 at 21:24

2 Answers 2

2

try

@foobar_collect['items'][0]['user_id']

The reason why your code doesn't work is @foobar_collect['items'] is an array.

Sign up to request clarification or add additional context in comments.

2 Comments

Is there a difference between "" and '' and what is the difference between .first and [0]
no there's no difference, i only use double quotes when a string has a ruby code inside the string like "asd#{1 + 1}asd". [0] and .first is also the same but I prefer [0] since it's easier to read and easier to get other elements of the array.
0
@foobar_collect["items"].first["user_id"]

3 Comments

Is there a difference between "" and '' and what is the difference between .first and [0]
@Elementmindfreak No meaningful different between .first and [0] in this case. Double-quoted strings are interpolated, single-quoted strings aren't.
@Elementmindfreak to expound upon @DaveNewton's answer: interpolation means you can evaluate an expression inside a double-quoted string using #{...}. For example: name = 'Pete'; puts "Hello, #{name}" # => 'Hello, Pete!'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.