JSON question: I'm wanting to use Delphi's latest JSON Builder to create a JSON expression that is structured like this:

JSON question: I'm wanting to use Delphi's latest JSON Builder to create a JSON expression that is structured like this:

NumStartMethods: integer
StartMethodUsed: integer
StartMethods:
Method1
param1 = something1
param2 = something2
Method2
param1 = something3
param2 = something4
param3 = something5

So we have two pairs, and an array of objects.

But I can't figure out how to get an array of objects to work!

{
"Bookmark": {
"NumStartMethods": 2,
"StartMethodUsed": 1,
"StartMethods": [
"StartMethod-1": {
"aID": "",
"aName": "foo"
},
"StartMethod-2": {
"aID": "xyz",
"aName": "bar"
"aFlag": false
}
]
}
}

This doesn't work.

Changing the square brackets to curly brackets works, but then StartMethods is just an object that contains objects, which is not what I want.

Builder
.BeginObject
.BeginObject('Bookmark')
.Add('NumStartMethods', 1)
.Add('StartMethodCalled', 1)
//.BeginArray('StartMethods')
.BeginObject('StartMethod-1')
.Add('aID', aID )
.Add('aName', aName)
.EndObject
.BeginObject('StartMethod-2')
.Add('aID', 'xyz' )
.Add('aName', aName)
.Add('aLoop', False )
.EndObject
//.EndArray
.EndObject
.EndObject;


I want to be able to access StartMethods[ StartMethodUsed ] to figure out which set of parameters are needed.

Maybe I'm just thinking about this wrong?

Comments

  1. jsonlint.com - The JSON Validator can show you where the Json doesn't "add up".

    A list has to be a list of either base types (int, string, etc), or a list of objects. I would do it like this:

    {
    "Bookmark": {
    "NumStartMethods": 2,
    "StartMethodUsed": 1,
    "StartMethods": [{
    "MethodName": "StartMethod1",
    "aID": "",
    "aName": "foo"
    },
    {
    "MethodName": "StartMethod2",
    "aID": "xyz",
    "aName": "bar",
    "aFlag": false
    }
    ]
    }
    }

    I'd also consider changing the StartMethodUsed to refer to MethodName, so that you don't get into list reordering situations.

    ReplyDelete
  2. Also note that dealing with polymorphic object lists usually require a bit of "manual intervention" with regards to the built in Delphi methods.

    ReplyDelete
  3. Thanks. Unfortunately, the start method names are the same (overloaded). But in adding the code that generates this JSON data, I realized this isn't the correct approach anyway. I know some of these forms have multiple start methods that take different parameter lists. I'm just not sure yet how to figure out what to throw at them. But it's a great use of JSON data structures!

    ReplyDelete

Post a Comment