It seems that this JSON parsing is quite Confusing in a way and hard to get it properly .
I have a JSON coming from an API and it has few parts that I need to get and its confusing here as once I use the ParseJSON I get an array with I element and inside that element I have 6 entries and some of them are as well Dictionaries so how do I reach to the final point ? and having arrays of dictionaries in the values is confusing for me, do I have to take each key and scan the value ?
At its outermost layer, that JSON describes an array with one element, a Dictionary with six keys, so Xojo is returning the value exactly as it should.
dim arr() as auto = Xojo.Data.ParseJSON( jsonText )
dim mainDict as Xojo.Core.Dictionary = arr( 0 )
dim label as text = mainDict.Value( "label" )
dim exportConfig as Xojo.Core.Dictionary = mainDict.Value( "exportConfiguration" )
// etc
Note: The Dictionary returned by ParseJSON is case-sensitive.
[quote=374733:@Kem Tekinay]At its outermost layer, that JSON describes an array with one element, a Dictionary with six keys, so Xojo is returning the value exactly as it should.
dim arr() as auto = Xojo.Data.ParseJSON( jsonText )
dim mainDict as Xojo.Core.Dictionary = arr( 0 )
dim label as text = mainDict.Value( "label" )
dim exportConfig as Xojo.Core.Dictionary = mainDict.Value( "exportConfiguration" )
// etc
Note: The Dictionary returned by ParseJSON is case-sensitive.[/quote]
Hi Kem, thanks for the tips, I did started to work with Introspection and to get the Get Type of each Value to know if I have to switch to levels and it works so far but I get an issue . it seems that Xojo.Core.Dictionary.HasKey does not work or at least not here.
I put brake points to see the data , I put a test for example :
[code] Dim L1Dict As Xojo.Core.Dictionary
L1Dict = entry.Value
If L1Dict.HasKey("id") Then
Dim idDict As Xojo.Core.Dictionary
idDict = L1Dict.Value("id")
gfcDataHolder.idName = idDict.Value("name")
gfcDataHolder.idVersion = idDict.Value("version")
End If[/code]
where L1Dict is the main dictionary in my case and I see in the Variables that the Key is “id” but on the logics it skips If L1Dict.HasKey("id") Then as it was false, and same thing with all the values.
[code]Dim json As Auto
Dim jsonArray() As Auto
Dim contentC() As Auto
Dim dict As Xojo.Core.Dictionary
gfcDataHolder = New GFC
json = Xojo.Data.ParseJSON(App.SampleJSON)
Try
jsonArray = json
dict = jsonArray(0)
For Each entry As Xojo.Core.DictionaryEntry In dict
Dim RecordKey As String
Dim RecordValue As String
Dim info As Xojo.Introspection.TypeInfo
info = Xojo.Introspection.GetType(entry.Value)
If info.Name = "Text" Then
Select Case entry.Key
Case "label"
gfcDataHolder.label = entry.Value
Case "description"
gfcDataHolder.description = entry.Value
End Select
Elseif info.Name = "Dictionary" Then
'--------------------------
Dim subm As Xojo.Core.Dictionary
subm = entry.Value
RecordKey = entry.Key
RecordValue = "Is A Dictionary"
'--------------------------
Dim L1Dict As Xojo.Core.Dictionary
L1Dict = entry.Value
If L1Dict.HasKey("id") Then
Dim idDict As Xojo.Core.Dictionary
idDict = L1Dict.Value("id")
gfcDataHolder.idName = idDict.Value("name")
gfcDataHolder.idVersion = idDict.Value("version")
End If
If L1Dict.HasKey("functionalConfiguration") Then
Dim fcDict As Xojo.Core.Dictionary
fcDict = L1Dict.Value("functionalConfiguration")
gfcDataHolder.fcDocumentType = fcDict.Value("documentType")
gfcDataHolder.fcLifespan = fcDict.Value("lifespan")
gfcDataHolder.fcCanModifySender = fcDict.Value("canModifySender")
gfcDataHolder.fcCanModifyRecipients = fcDict.Value("canModifyRecipients")
gfcDataHolder.fcCanModifyAttachments = fcDict.Value("canModifyAttachments")
End If
If L1Dict.HasKey("fixedRecipient") Then
Dim frDict As Xojo.Core.Dictionary
frDict = L1Dict.Value("fixedRecipient")
gfcDataHolder.frIdentifier = frDict.Value("identifier")
gfcDataHolder.frQuality = frDict.Value("quality")
gfcDataHolder.frName = frDict.Value("name")
End If
End If
Next
Catch
End Try[/code]
I see under RecordKey = entry.Key the keys but on the HasKey Checking it always skips the code like its false even if its same name .
You can also do something like if info = GetTypeInfo( Text ) then .... That might be faster and is certainly future-proof if Xojo, for instance, changes the “Text” type to “Unicode”. (They wouldn’t, this is for illustration only.)