ptypes

JSON-Example


How to use JSON with ptypes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <ptypes.h>
#include <pstreams.h>
 
USING_PTYPES;
 
// Well...this is hacky...
#define MULTILINE(...) #__VA_ARGS__
#define TPARR_CAST(V) (tparray*)(pt::component*) V
 
int main() {
    string json = MULTILINE(
        {"menu": {
  "id": "file",
  "value": "File",
  "popup": {
    "menuitem": [
      {"value": "New", "onclick": "CreateNewDoc()"},
      {"value": "Open", "onclick": "OpenDoc()"},
      {"value": "Close", "onclick": "CloseDoc()"}
    ]
  }
}}
        );
    pt::json j;
    variant data = j.JsonDecode(json);
    variant val, key;
    tparray* menu = TPARR_CAST(get(*(TPARR_CAST(data)), "menu"));
 
    pout.put(get(*menu, "id"));
    pout.put("\n");
 
 
    for(int i = 0; pt::anext(pt::at(*(TPARR_CAST(data)), 0), i, val, key);)
    {
        if (isstring(val)) {
            pout.put("\n");
            pout.put(key);
            pout.put(" => ");
            pout.put(val);
        }
    }
 
    pout.put("\n");
}

Output:

file

id => file
value => File

Created: 9 years 5 months ago
by Natalie Adams

Labels:
Other:Featured

Page rendered in 0.06109s using 24 queries.