-
Notifications
You must be signed in to change notification settings - Fork 583
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Function ConvertToJson work not correctly when the string contain continuous zero #236
Comments
VBA-JSON depends on how you format your Excel cells to determine the datatype to assign in JSON. If you don't specify a format for a cell, then it is General which essentially will be format Number when entering Public Sub test()
Dim Worksheet As Excel.Worksheet
Dim jsonArray As New VBA.Collection
Dim json As String
Set Worksheet = ActiveSheet
jsonArray.Add Worksheet.Range("A1").Value ' Cell formated as General.
jsonArray.Add Worksheet.Range("B1").Value ' Cell formatted as Number, zero decimal places.
jsonArray.Add Worksheet.Range("C1").Value ' Cell formatted as Text.
json = ConvertToJson(jsonArray, Whitespace:=2)
Debug.Print json
Exit Sub
End Sub This code will print the following the Developer Immediate window in the VBA project environment: [
12020000000000,
12020000000000,
"0012020000000000"
] So what I suspect your issue is, is that you have not appropriately set the format for cell in question and this is not a bug. Hope that helps. |
I noticed that your GitHub account had a repository with a workbook in it that contains some example data. I downloaded that and did a little digging into the VBA-JSON code which indicates in Case VBA.vbString
' String (or large number encoded as string)
If Not JsonOptions.UseDoubleForLargeNumbers And json_StringIsLargeNumber(JsonValue) Then
ConvertToJson = JsonValue
Else
ConvertToJson = """" & json_Encode(JsonValue) & """"
End If Your cells in column C have Public Sub test()
Dim Worksheet As Excel.Worksheet
Dim jsonArray As New VBA.Collection
Dim json As String
Set Worksheet = ActiveSheet
jsonArray.Add Worksheet.Range("C2").Value
jsonArray.Add Worksheet.Range("C3").Value
jsonArray.Add Worksheet.Range("C4").Value
JsonOptions.UseDoubleForLargeNumbers = True
json = ConvertToJson(jsonArray, Whitespace:=2)
Debug.Print json
Exit Sub
End Sub which produces the following the Developer Immediate window in the VBA project environment: [
"0012020000000000",
"0012020000000000",
"0012020000000000"
] Your issue doesn't appear to be a bug, but you stumbled upon a feature!! |
Hi houghtonap, VBA only stores 15 significant digits, so any numbers larger than that are truncated Really thanks for your kindly reply~! |
There are a number of open issues about BTW note that there is a small performance issue with that |
I agree with @Nick-vanGemeren about However, the bigger underlying question for me is why my test code was successful using the same string as @badoyau, but it failed in the workbook @badoyau was using. It wasn't until I downloaded @badoyau workbook that I could see it failing. My environment is Windows 10, Excel 2016 and I was using VBA-JSON v2.3.0. However, the code, in this area, looks the same between v2.3.0 and the current version. |
Hi,
Thank for your library. it is a useful library for my project.
i have one item as string in excel cells as 0012020000000000
but when i use dictionary call final and export as json by json1 = JsonConverter.ConvertToJson(Final, Whitespace:=2)
it will return in differenct types of results. SO i try many cases such as following
0012020000000000 , then it return 0012020000000000 as integer, instead "0012020000000000"
001202000000000 , then it work perfectly as "001202000000000"
001202000000000A , then it work perfectly as "001202000000000A"
please kindly check this bug for many thanks!
The text was updated successfully, but these errors were encountered: