Problem parsing JSONItem

I am \trying to parse a JSON Item whose ToString property is

The debugger reveals IsArray is True and HasName(“Item”) is false and this code fails with a TypeMismatchException

dim rec as new JSONItem rec = fileData.Child(j) dim aArray As Boolean = rec.IsArray dim aName As Boolean = rec.HasName("Item") try dim aVal As String = rec.Value(1) Catch e as RuntimeException dim t As Introspection.TypeInfo = Introspection.GetType(e) break end try
fileData is a JSONItem with several records in it consisting of Item and ItenVal.[quote][[{“Item”:“FacCode”},{“ItemVal”:“109150016”}],[{“Item”:“LastSync”},{“ItemVal”:“2020-01-06 17:40:22”}]][/quote]

What is the proper way to get these two values?

I find the easiest way to use JSON is to use the parsejson function to convert the data to a dictionary, then I can iterate through the entries.

The TypeMismatchException is because an object item returns a JSONItem, not a String.
You should absolutely not be using a catch all exceptions here. Catch JSONException.

What kind of array? You should only be adding Variant arrays.

From memory, I think I made JSONItem_MTC more tolerant.

https://github.com/ktekinay/JSONItem_MTC

Here’s how you can do it using the old new way (2019r1.1). Don’t know about the new new way.

[code]dim t as text = “[[{”“Item”":"“FacCode”"},{"“ItemVal”":"“109150016"”}],[{"“Item”":"“LastSync”"},{"“ItemVal”":"“2020-01-06 17:40:22"”}]]"

dim a(), b() as auto
dim d as Xojo.Core.Dictionary

a = Xojo.Data.ParseJSON(t)

for each b in a
for each d in b

if d.HasKey("Item") then
  MsgBox(d.Value("Item"))
end if

next
next
[/code]


Tey
dim rec as new JSONItem
Dim item as jsonitem
rec = fileData.Child(j)
dim aArray As Boolean = rec.IsArray
Item = rec.valueat(0) // first array item
dim aName As Boolean = item.HasName("Item") // should be true

//  dim aVal As String = rec.Value(1)
Catch e as RuntimeException
  dim t As Introspection.TypeInfo = Introspection.GetType(e)
  break
end try

Ug, ignore my answer, I read your code too fast.

Thanks everybody. I had missed that I had another JSONItem embedded in my rec object.