ExtractJSONStructure() problem

Just starting out? Need help? Post your questions and find answers here.
infratec
Always Here
Always Here
Posts: 6866
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

ExtractJSONStructure() problem

Post by infratec »

Hi,

I have such a JSON:
{
"name": "hauptprogramm",
"headers": {
"Datei" : [{
"name": "Mandant wechseln",
"showIcon": true,
"iconName": "Icon_Change"
},
{
"name": "Beenden",
"showIcon": true,
"iconName": "Icon_Exit"
}]
}
}
In Code:

Code: Select all

JSON$ = ~"{" + #LF$
JSON$ + ~"  \"name\": \"hauptprogramm\"," + #LF$
JSON$ + ~"  \"headers\": {" + #LF$
JSON$ + ~"    \"Datei\" : [{" + #LF$
JSON$ + ~"      \"name\": \"Mandant wechseln\"," + #LF$
JSON$ + ~"      \"showIcon\": true," + #LF$
JSON$ + ~"      \"iconName\": \"Icon_Change\"" + #LF$
JSON$ + ~"    }," + #LF$
JSON$ + ~"    {" + #LF$
JSON$ + ~"      \"name\": \"Beenden\"," + #LF$
JSON$ + ~"      \"showIcon\": true," + #LF$
JSON$ + ~"      \"iconName\": \"Icon_Exit\"" + #LF$
JSON$ + ~"    }]" + #LF$
JSON$ + ~"  }" + #LF$
JSON$ + ~"}"
Current structures:

Code: Select all

Structure JsonMenuEntry
  name.s
  showIcon.i
  iconName.s
EndStructure

Structure JsonMenuHeader
  List Datei.JsonMenuEntry()
  List Bearbeiten.JsonMenuEntry()
EndStructure

Structure JsonMenuTemplate
  name.s
  headers.JsonMenuHeader
EndStructure
My problem is, that "Datei" should be a map member an not a known fixed element.
"Datei" and "Bearbeiten" should not be in the structure.

My only solutions needs an additional name

Code: Select all

Structure JsonMenuEntry
  name.s
  showIcon.i
  iconName.s
EndStructure

Structure JsonMenuHeader
  List A.JsonMenuEntry()
EndStructure

Structure JsonMenuTemplate
  name.s
  Map headers.JsonMenuHeader()
EndStructure

Define.JsonMenuTemplate template

template\name = "hauptprogramm"

AddMapElement(template\headers(), "Datei")
AddElement(template\headers()\A())
template\headers()\A()\iconName = "Icon_Change"
template\headers()\A()\name = "Mandant wechseln"
template\headers()\A()\showIcon = #True

AddElement(template\headers()\A())
template\headers()\A()\iconName = "Icon_Exit"
template\headers()\A()\name = "Beenden"
template\headers()\A()\showIcon = #True

If CreateJSON(0)
  InsertJSONStructure(JSONValue(0), @template, JsonMenuTemplate)
  Debug ComposeJSON(0, #PB_JSON_PrettyPrint)
EndIf
The 'A' is wrong.

I want to use ExtractJsonStructure() to get the JSON$ in an easy way to accessible data.

Any idea to solve this task?

Bernd
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: ExtractJSONStructure() problem

Post by #NULL »

If you have a Map of Lists, then the List has to be named somehow too (I believe), like A() for example.
But maybe using a Map of Maps instead makes it look better, in case you are in controll of the json structure:

Code: Select all

Structure JsonMenuEntry
  ;name.s
  showIcon.i
  iconName.s
EndStructure

Structure JsonMenuHeader
  Map name.JsonMenuEntry()
EndStructure

Structure JsonMenuTemplate
  name.s
  Map headers.JsonMenuHeader()
EndStructure

Define.JsonMenuTemplate template

template\name = "hauptprogramm"

template\headers("Datei")\name("Mandant wechseln")\iconName = "Icon_Change"
template\headers("Datei")\name("Mandant wechseln")\showIcon = #True

template\headers("Datei")\name("Beenden")\iconName = "Icon_Exit"
template\headers("Datei")\name("Beenden")\showIcon = #True

template\headers("Bearbeiten")\name("Copy")\iconName = "Icon_Copy"
template\headers("Bearbeiten")\name("Copy")\showIcon = #True

template\headers("Bearbeiten")\name("Paste")\iconName = "Icon_Paste"
template\headers("Bearbeiten")\name("Paste")\showIcon = #True

If CreateJSON(0)
  InsertJSONStructure(JSONValue(0), @template, JsonMenuTemplate)
  Debug ComposeJSON(0, #PB_JSON_PrettyPrint)
EndIf
Post Reply