Ruby on Rails Friday, February 8, 2019



On Wednesday, January 30, 2019 at 5:16:59 PM UTC-5, Colin Law wrote:
On Wed, 30 Jan 2019 at 22:12, Colin Law <cla...@gmail.com> wrote:
>
> On Wed, 30 Jan 2019 at 22:09, fugee ohu <fuge...@gmail.com> wrote:
> >
> >
> >
> > On Wednesday, January 30, 2019 at 5:02:17 PM UTC-5, Colin Law wrote:
> >>
> >> On Wed, 30 Jan 2019 at 21:56, fugee ohu <fuge...@gmail.com> wrote:
> >> > ...
> >> > Everything in the unparsed resonse body that I want is between [ and ] I have to gsub it out
> >>
> >>
> >> No you don't.  After you get parsed_obj["results] (which is an array,
> >> that's what the [] mean) then you can get the first product by
> >> parsed_obj["results"][0]["productId"]
> >> It is just an array.  You have met ruby arrays haven't you?
> >>
> >> I am rapidly losing the will to live.
> >>
> >> Colin
> >
> >
> > The response body isn't JSON.parse parsable as is it has to be gsub'd and chomped first before I can run JSON.parse My original gsub wasn't right it wasn't removing the end that follows ]
> > JSON::ParserError: 784: unexpected token at 'myscript.js({"success":true,"code
>
> You previously posted that you had got parsed_obj where
> parsed_obj["results]  was an array.  Go back to that.

To quote your previous message

>puts parsed_obj["results"]  shows the entire results but `puts parsed_obj["results"]["productId"] gets me error no implicit
> conversion of String into Integer

The error is because it is an array, which is perfectly obvious if you
look at the unparsed string. So if you use
parsed_obj["results"][0]
you will get the first element

Colin

Now I get this error if I use JSON.parse or JSON.load
JSON::ParserError: 784: unexpected token at ',{"skuAttr":"200007763:201336106;491:200004763#145cm","skuPropIds":"201336106,200004763","skuVal":{"actSkuBulkCalPrice":"313.64","actSkuCalPrice":"330.15","actSkuDisplayBulkPrice":"US $313.64","actSkuMultiCurrencyBulkPrice":"313.64","

doc.css("script").each do |page|
if page.text=~/skuProducts/
       skudata = page.text[/var skuProducts=\[(.+?)\];/, 1]
               data = JSON.parse(skudata)
               puts data
end
end

--
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 rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/22695008-7fe2-4fa7-b7ed-3980acd885c6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment