prabhu wrote in post #1145452:
> I have a string object which is basically in a json format and while
> trying to print it shows in console as
>
>
>     item =
>      {
>             "id": "4c9f83e4-f479-48d0-9f92-3fff70a8f6ba",
>              "item":
> 
"{"business":"1114","class":"Demo","date":"01-01-2014","version":"","data":"dummy","name":"Finance"}"
>
>         }
>
> I need to get the values of business, class, date etc and pass it as
> params
> to my method. So I tried to convert it into hashes as below
>
>     hash_item = JSON.parse (item)
>
> and output in console shows as
>
>     The converted hash item is
>     {"guid"=>"4c9f83e4-f479-48d0-9f92-3fff70a8f6ba",
> 
"item"=>"{"business":"1114","class":"Demo","date":"01-01-2014","version":"","data":"Dummy","name":"Finance"}"}
>
> But when I try to access the hash value for business as
> `hash_item['item']['business']` it shows
>
>> "business"
>
>  since the value of item is a String in the hash_item. I am not sure
> whether my approach is correct or not. So is there any better idea or
> any
> inputs to retrieve the hash values . Please help.

Let me try to explain by example...

JSON demo 1:

{
  "id":  "4c9f83e4-f479-48d0-9f92-3fff70a8f6ba",
  "item": {
    "business": "1114",
    "class": "Demo",
    "date": "01-01-2014"
  }
}

item = "{\"id\":  \"4c9f83e4-f479-48d0-9f92-3fff70a8f6ba\",\"item\": 
{\"business\": \"1114\",\"class\": \"Demo\", \"date\": \"01-01-2014\"}}"

json = JSON.parse(item)
=> {"id"=>"4c9f83e4-f479-48d0-9f92-3fff70a8f6ba", 
"item"=>{"business"=>"1114", "class"=>"Demo", "date"=>"01-01-2014"}}

json['id']
=> "4c9f83e4-f479-48d0-9f92-3fff70a8f6ba"

json['item']
=> {"business"=>"1114", "class"=>"Demo", "date"=>"01-01-2014"}

json['item']['business']
=> "1114"

json['item']['class']
=> "Demo"

JSON Demo 2:

{
  "id":  "4c9f83e4-f479-48d0-9f92-3fff70a8f6ba",
  "item": "{"business": "1114","class": 
"Demo", "date": "01-01-2014""
  }"
}

item = "{\"id\":  \"4c9f83e4-f479-48d0-9f92-3fff70a8f6ba\", \"item\": 
\"{"business": "1114", "class": 
"Demo", "date": "01-01-2014"}\"}"

json = JSON.parse(item)
=> {"id"=>"4c9f83e4-f479-48d0-9f92-3fff70a8f6ba", 
"item"=>"{"business": "1114", "class": 
"Demo", "date": "01-01-2014"}"}

json['id']
=> "4c9f83e4-f479-48d0-9f92-3fff70a8f6ba"

json['item']
=> "{"business": "1114", "class": 
"Demo", "date": "01-01-2014"}"

json['item']['business']
=> "business"

It appears to me you're seeing something like "JSON Demo 2". What you 
want is what you see in "JSON Demo 1".

-- 
Posted via http://www.ruby-forum.com/.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/cf43e09d59a670d6697fe3019cc02fa7%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to