# VFP用Foxjson玩转JSON,超简单的教程
这段时间忙于实现小程序的树型结构,照例是上网抄抄抄、本地试试试,摸了好几天,结果树型结构只做了个半成品,却把foxjson建立JSON的方法玩通了。真是无心插柳柳成阴啊!因此和大家分享一下心得。
# 一、不管多少级,每一级都要有元素和元素数组。
假设有3级,那么每一级如下:
Ones=createobject(“foxjson”,{})
One=createobject(“foxjson”)
Twos=createobject(“foxjson”,{})
Two=createobject(“foxjson”)
Threes=createobject(“foxjson”,{})
Three=createobject(“foxjson”)
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 二、赋值顺序为:下一级元素--->下一级元素数组--->上一级元素--->上一级元素数组
元素的赋值用append(健值对),元素数组的赋值用append(元素)
# 三、赋值采取“吃吐大法”(我觉得用吃吐比较形象)。
元素吃完键值对,要吐给元素数组;元素数组吃完所有元素要吐给上一级元素。吐完都要重新建立才能再吃,就这样重复吃吐直到所有元素和元素数组都吃到第一级元素数组为止。
Ones=Createobject(“foxjson”,{})
One=Createobject(“foxjson”)
Twos=Createobject(“foxjson”,{}) &&&&&&第二级建立&&&&&&&
two=Createobject(“foxjson”) &&&&&&第二级建立&&&&&&&
threes=Createobject(“foxjson”,{}) &&&&&&第三级建立&&&&&&&
three=Createobject(“foxjson”) &&&&&&第三级建立&&&&&&&
three.Append(“title”,”第三级1”) &&&&&&第三级吃&&&&&&&
three.Append(“children”,[]) &&&&&&第三级吃&&&&&&&
threes.Append(three) &&&&&&第三级吐&&&&&&
three=Createobject(“foxjson”) &&&&&&第三级建立&&&&&&
three.Append(“title”,”第三级2”) &&&&&&第三级吃&&&&&&&
three.Append(“children”,[]) &&&&&&第三级吃&&&&&&&
threes.Append(three) &&&&&& 第三级吐&&&&&&
two.Append(“title”,”第二级1”) &&&&&&&第二级吃&&&&&&
two.Append(“children”,threes) &&&&&&&第二级吃&&&&&&
twos.Append(two) &&&&&&&第二级吐&&&&&&
two=Createobject(“foxjson”) &&&&&&&&重复上面操作&&&&&&&&&
threes=Createobject(“foxjson”,{})
three=Createobject(“foxjson”)
three.Append(“title”,”第三级3”)
three.Append(“children”,[])
threes.Append(three)
three=Createobject(“foxjson”)
three.Append(“title”,”第三级4”)
three.Append(“children”,[])
threes.Append(three)
two.Append(“title”,”第二级2”)
two.Append(“children”,threes)
twos.Append(two)
one.Append(“title”,”第一级1”) &&&&&&&第一级吃&&&&&&
one.Append(“children”,twos) &&&&&&&第一级吃&&&&&&
ones.Append(one) &&&&&&&第一级吐&&&&&&
?ones.tostring()
_Cliptext=ones.tostring()
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
将结果发到https://www.json.cn/验证也是OK,如下图。
[
{
**"title"**:**"****第一级****1"**,
**"children"**:[
{
**"title"**:**"****第二级****1"**,
**"children"**:[
{
**"title"**:**"****第三级****1"**,
**"children"**:**[]**
},
{
**"title"**:**"****第三级****2"**,
**"children"**:**[]**
}
]
},
{
**"title"**:**"****第二级****2"**,
**"children"**:[
{
**"title"**:**"****第三级****3"**,
**"children"**:**[]**
},
{
**"title"**:**"****第三级****4"**,
**"children"**:**[]**
}
]
}
]
}
]
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
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
看了上图大家可能有疑问,JSON树体现了各级的元素,那元素数组在树的哪里体现出来呢?其实很简单,上图中的第一级、第二级的children后中括号[]就是表示设有元素数组,如果少了[],说明下级元素直接添加给了上级元素,json验证也会通过,但个人觉得不够规范。
接下来,再进一步的想法就是中间层根据数据表生成json树再传回小程序,中间层接口代码如下:
例子中的pbtz表的operator,linename,cardname三个字段分别代表父、子、孙三个节点的值
Procedure getpbtree lcregion=HttpQueryParams("region",This.iconnid)
TEXT TO lcSQLCmd NOSHOW TEXTMERGE
SELECT id,operator,linename,cardname FROM pbtz ORDER BY operator,linename,cardname
ENDTEXT oDBSQLHelper=NEWOBJECT("MSSQLHelper","MSSQLHelper.prg")
If oDBSQLhelper.SQLQuery(lcSQLCmd,"pbtz_temp")<0
Error oDBSQLhelper.errmsg
Endif
***********************************************************
Select pbtz_temp
Update pbtz_temp Set operator=Alltrim(operator),linename=Alltrim(linename),cardname=Alltrim(cardname)
=Tableupdate(.T.)
Select pbtz_temp
Go Top
loperator=pbtz_temp.operator
llinename=pbtz_temp.linename
lcardname=pbtz_temp.cardname
oSchools=Createobject("foxjson",{})
oSchool=Createobject("foxjson")
oSchool.Append("title",loperator)
oSchool.Append("ispb","0")
oClasss=Createobject("foxjson",{})
oClass=Createobject("foxjson")
oClass.Append("title",llinename)
oClass.Append("ispb","0")
oStus=Createobject("foxjson",{})
oStu=Createobject("foxjson")
oStu.Append("title",lcardname)
oStu.Append("ispb","1")
oStu.Append("children",[])
oStus.Append(oStu)
Skip
For i=1 To 1000 &&这地方代码有点low,因为表中945条记录,所以定i最大值为1000&&&&&&
If Eof()
Exit
Endif
If pbtz_temp.operator=loperator And pbtz_temp.linename=llinename &&第一级第二级节点不变时
oStu=Createobject("foxjson")
oStu.Append("title",pbtz_temp.cardname)
oStu.Append("ispb","1")
oStu.Append("children",[])
oStus.Append(oStu)
Endif
If pbtz_temp.operator=loperator And pbtz_temp.linename<>llinename &&第一级不变,第二级变时
oClass.Append("children",oStus)
oClasss.Append(oClass)
llinename=pbtz_temp.linename
oClass=Createobject("foxjson")
oClass.Append("title",pbtz_temp.linename)
oStus=Createobject("foxjson",{})
oStu=Createobject("foxjson")
oStu.Append("title",pbtz_temp.cardname)
oStu.Append("ispb","1")
oStu.Append("children",[])
oStus.Append(oStu)
Endif
If pbtz_temp.operator<>loperator &&第一级变时
oClass.Append("children",oStus)
oClasss.Append(oClass)
oSchool.Append("children",oClasss)
oSchools.Append(oSchool)
loperator=pbtz_temp.operator
llinename=pbtz_temp.linename
lcardname=pbtz_temp.cardname
oSchool=Createobject("foxjson")
oSchool.Append("title",pbtz_temp.operator)
oClasss=Createobject("foxjson",{})
oClass=Createobject("foxjson")
oClass.Append("title",pbtz_temp.linename)
oStus=Createobject("foxjson",{})
oStu=Createobject("foxjson")
oStu.Append("title",pbtz_temp.cardname)
oStu.Append("ispb","1")
oStu.Append("children",[])
oStus.Append(oStu)
Endif
Skip
Endfor
oClass.Append("children",oStus)
oClasss.Append(oClass)
oSchool.Append("children",oClasss)
oSchools.Append(oSchool)
*oSchools.tostring()
********************************************************************
Return '{"errno":0,"errmsg":"ok","filename":'+oSchools.tostring()+'}'
Endproc
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
小程序接收后显示的树型结构如下图。
小程序接收显示我是参考网上大侠的代码:
https://blog.csdn.net/weixin_44646763/article/details/122751392