name: Video
shortDescription: My video
thumbnailURL: http://...................../myimage.jpg
videos: <Component: roArray>
id: 3.171e+12
regex = CreateObject("roRegex", ":(\s?)([0-9]{9,})", "")
newJson = regex.ReplaceAll(json, ":\1" + Chr(34) + "\2" + Chr(34))
"TheEndless" wrote:
Most likely, the ID is a 64-bit integer in your JSON, which gets parsed as a float by Roku's ParseJSON function, often resulting in a completely different value. The only way I've found to work around it is to convert the values to strings before parsing. If you need it as an integer, then you can convert it back in BrightScript.
Here's the RegEx I use to convert any number in the JSON string that's nine digits or longer to a string:regex = CreateObject("roRegex", ":(\s?)([0-9]{9,})", "")
newJson = regex.ReplaceAll(json, ":\1" + Chr(34) + "\2" + Chr(34))
BrightScript Debugger> ? 2^30, 2^31, 2^32
1073741824 -2147483648 0
BrightScript Debugger> l = parsejson("[999999999,9999999999]")
BrightScript Debugger> ? l
999999999
1e+10
BrightScript Debugger> ? type(l[0]), type(l[1])
Integer Float
"EnTerr" wrote:
Ha! That's a head-scratcher. It is worth mentioning that int`s are 32-bit in BS, no bignums and parseJSON() is behaving reasonably:
Function AsDouble(input As Dynamic) As Double
output# = 0
If IsString(input) Then
If input.Len() <= 9 Then
output# = input.ToInt()
Else
' Big string, so break it into parts and build the double
low = input.Mid(input.Len() - 9, 9).ToInt()
high = input.Mid(0, input.Len() - 9).ToInt()
output# = high
output# = output# * 1000000000
output# = output# + low
End If
Else If IsInteger(input) Or IsFloat(input) Then
output# = input
End If
Return output#
End Function
"EnTerr" wrote:
miss cases where \t or multiple spaces are present between : and the number (in dictionary)
:(\s*)([0-9]{9,})
"EnTerr" wrote:
- miss numbers within a list, e.g. [1, 1234567890, 3]
- mangle strings that contain long numbers preceded by :, e.g. {"text": "this is how a 10-digit number looks like: 1234567890"}
"TheEndless" wrote:
Perhaps, but parsing as a Double instead of a Float seems to give better results. I recently had to overcome this in some JSON that had date/times in milliseconds, using this custom AsDouble() hack to convert the string after the regex replace...
BrightScript Debugger> xstr="4294967296000"
BrightScript Debugger> eval("x="+xstr)
BrightScript Debugger> ?x
4294967296000
BrightScript Debugger> ?type(x)
Double
"renojim" wrote:
In the past, when I've needed to convert a string representing a large integer into a Double I've used the following trick:BrightScript Debugger> xstr="4294967296000"
BrightScript Debugger> eval("x="+xstr)
BrightScript Debugger> ?x
4294967296000
BrightScript Debugger> ?type(x)
Double
-JT
"TheEndless" wrote:
From what I've been told, though, using Eval() causes an inherent memory leak, so it should be avoided whenever possible
left(msTimeStamp, 10).toInt()
We’re upgrading Roku Community to bring you a faster, more mobile-friendly experience. You may notice limited functionality or read-only access during this time. You will not be able to log in or post new comments or kudos during this time. Read more here.
Planned Downtime:
Community will be unavailable for up to 24–48 hours during the upgrade window during the week of May 12 and you may notice reduced functionality.
In the meantime, for additional assistance, visit our Support Site.
Thanks for your patience — we’re excited to share what’s next!