I have some json that has integers in it such as quantity, amount etc… I need to display some of these values, since they are not quoted (" ") in the json they come out as type Int32. To display it I have been using the following code
Dim Amt as Int32
Amt = Order.Value(“amount”)
While this works fine it’s a bit tedious if you have many numbers in the json. Is there a better way to handle the conversion from number to text? If anybody is wondering MsgBox(Order.Value(“amount”).ToText) does not work.
If I had to guess, the end goal is not to display them all in multiple MsgBoxes, and that we’re looking for a usable string/text value for the amount? Both solutions equally succinct, neither need to deal with the Integer value.
Old framework (JSONItem and String):
dim sOrderAmount as String = JSONItem.Lookup("amount", 0).StringValue
[s]New Framework (Xojo.Core.Dictionary and Text):
dim txOrderAmount as Text = DictonaryObject.Lookup("amount", 0)
Guess I don’t use the new framework enough to answer that.
[quote=371584:@Tim Parnell]So it sounds like the new framework is less succinct.
You have to test what “amount” is, because who knows that it’s always going to be an integer, then shove it into the appropriate datatype, then use that datatype .ToText?[/quote]
That’s basically it. My succinct comment was very much sarcasm.
There’s a lot of using Introspection with Auto in the new framework. Which also doesn’t seem to ever release the introspection objects, according to the runtime object list.
So the most “correct” way to do it is something like:
Dim Value As Auto = Order.Value("amount")
Dim ValueInfo As Xojo.Introspection.TypeInfo = Xojo.Introspection.GetType(Value)
Select Case ValueInfo.FullName
Dim TextValue As Text = Value
Dim UInt32Value As UInt32 = Value
Dim Int32Value As Int32 = Value
And so on. You could get any of Double, [U]Int(8|16|32|64), or technically just about anything. It’s pretty horrifying. But it’s generally easier to cut corners and write exception handlers in case something doesn’t meet expectations.
Oh and for the record, casting from Auto to Int32 does not work. So even my original proposal won’t work. At this point, it would be easier to write some kind of AutoToInteger function:
Public Function AutoToInteger(Value As Auto) as Integer
#Pragma BreakOnExceptions Off
Dim IntValue As Integer
IntValue = Value
Catch Err As TypeMismatchException
IntValue = 0
Yeah, it really is easier just to use Try blocks to attempt getting the object into a potential class. But certain casting does work, for example you can put an Int64 into Auto and then put that Auto into Int32. So the only way to truly do the right thing is with introspection.
So when I try MsgBox(str(Order.Value(“amount”))) I get a compile message
There is more than one item with this name and it’s not clear to which this refers. It highlights “MsgBox(Str”
When I try MsgBox(Int32(Order.Value(amount)).ToText) I get a compile message
Type mismatch error, expected Int32 got Auto
So for clarification I am not displaying a bunch of numbers I am displaying a list of product orders which includes quantities, amounts price etc… That all has to be converted to display on screen either as a MsgBox, Label or Listbox. This information is coming ion as a json array of objects. Had the numbers been text this would have been simple.
The best suggestion I have seen is to write an extension… Still have to take multiple steps but it’s hidden and reusable. I’ll report back to confirm how it works.
Not everything coming from json is text. If the data is not inside quotes then it is treated as a number if in fact it’s a number. I believe it’s json good practice to use quotes on numbers if they are to be treated as text. The extension always delivers back Text regardless of the data type. My code is intricate and having something like separated out keeps it much easier to read and reuseable.
I understand that. I’m just wondering why you want to get the value as Text instead of String. Your Text values are just being auto-converted to String for display anyway. I would think (could be wrong) that String would be easier to get in the first place.