Hitting a wall with a JSONItem

Hi,

in a JSONItem I have this contents:

[]JSON
{}0
{}1
...
{14}
    name : "pk_ID"
    data : "108"
...

I would like to get the value of pk_ID which is 108, but I just can’t :-(.

I understand that the JSONItem is an array of Dictionary (or something else ?) . SO I do something like that (maybe in error):

Dim dictField As Xojo.Core.Dictionary
Dim AuFields As Auto

   dictField = xojo.data.ParseJSON(record.ToString.ToText)
   AuFields = dictField.Value("field")
...

But afterwards I just can’t get the needed value.

What should I do then ?

Many thanks

[code]DIM AuFields As Xojo.Core.Dictionary
AuFields = dictField.Value(“field”)

DIM pkID As integer
pkID = AuFields.Value(“data”)[/code]

Hi @shao sean,

thanks for your reply.

dictField.Value(“field”) returns an Auto, so the compiler throws an error on that line.

Some precision: the JSON sub-items are all of the same type and I don’t know which one has the “pk_ID” key, so I need to loop through them.

The way I displayed the JSON item is from which is a JSON viewer. The contents is this one:

[
  {
    "name": "fk_demande",
    "data": "36425"
  },
  {
    "name": "procedureID",
    "data": "3719-17"
  },
 <...>,
  {
    "name": "pk_ID",
    "data": "108"
  },
 [b] {
    "name": "PDF_image",
    "data": ""
  }[/b]
]

The next to the last one is the sub-item from which I need to get the “data” value.

AuFields = Xojo.Core.Dictionary(dictField.Value(“field”)) // gotta type cast it…

Post some real code and I’ll take a peek (or perhaps you’ll luck out and get someone smarter than me to help ya :wink: )

Ok here we go.

I query a FileMaker database using XFM from Tim Dietrich, that returns JSONItems.

...
Dim resultset As JSONItem
Dim record As JSONItem

resultset = FMResultSet.Value("resultset")

// This is the contents of resultset
// {
//   "count":"1",
//  "fetch-size":"1",
// "record":{
//     "mod-id":"0",
//      "record-id":"119",
//      "field":[ ]
//   }
//}
//
// I need to work with "field" under record

record = resultset.Value("record")

// This is the contents of record (some data is removed in field because of sensitive data)

{
   "mod-id":"0",
   "record-id":"119",
   "field":[
      {
         "name":"fk_demande",
         "data":"36425"
      },
      {
         "name":"procedureID",
         "data":"3719-17"
      },
    ...,
      {
         "name":"pk_ID",
         "data":"119"
      },
      {
         "name":"PDF_image",
         "data":""
      }
   ]
}

// Now I need to get the “field” part, and this is where it gets complicated for me.
// I may

Dim field As JSONItem
field = record.Value(“field”)

// Inside field is an array of dictionary, but I can’t find a way to get to the part where name is “pk_ID”.

[code]DIM dict As Xojo.Core.Dictionary = Xojo.Data.ParseJSON(self.TextArea1.Text.ToText)
DIM fields() As Auto = dict.Value(“field”)

for i as Integer = 0 to fields.Ubound
DIM temp As Xojo.Core.Dictionary = fields(i)
if (temp.Value(“name”) = “pk_ID”) then
MsgBox temp.Value(“data”)
end if
next[/code]

Well that worked, you make my day !

Thanks