前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >VFP用Foxjson玩转JSON,超简单的教程

VFP用Foxjson玩转JSON,超简单的教程

作者头像
加菲猫的VFP
发布2022-10-27 10:38:09
5720
发布2022-10-27 10:38:09
举报
文章被收录于专栏:加菲猫的VFP

作者:火种

这段时间忙于实现小程序的树型结构,照例是上网抄抄抄、本地试试试,摸了好几天,结果树型结构只做了个半成品,却把foxjson建立JSON的方法玩通了。真是无心插柳柳成阴啊!因此和大家分享一下心得。

一、不管多少级,每一级都要有元素和元素数组。

假设有3级,那么每一级如下:

代码语言:javascript
复制
Ones=createobject(“foxjson”,{})

One=createobject(“foxjson”)

Twos=createobject(“foxjson”,{})

Two=createobject(“foxjson”)

Threes=createobject(“foxjson”,{})

Three=createobject(“foxjson”)

二、赋值顺序为:下一级元素--->下一级元素数组--->上一级元素--->上一级元素数组

元素的赋值用append(健值对),元素数组的赋值用append(元素)

三、赋值采取“吃吐大法”(我觉得用吃吐比较形象)。

元素吃完键值对,要吐给元素数组;元素数组吃完所有元素要吐给上一级元素。吐完都要重新建立才能再吃,就这样重复吃吐直到所有元素和元素数组都吃到第一级元素数组为止。

代码语言:javascript
复制
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()

将结果发到https://www.json.cn/验证也是OK,如下图。

代码语言:javascript
复制
[
   {
     **"title"**:**"****第一级****1"**,
     **"children"**:[
       {
         **"title"**:**"****第二级****1"**,
         **"children"**:[
           {
             **"title"**:**"****第三级****1"**,
             **"children"**:**[]**
           },
           {
             **"title"**:**"****第三级****2"**,
             **"children"**:**[]**
           }
         ]
       },
       {
         **"title"**:**"****第二级****2"**,
         **"children"**:[
           {
             **"title"**:**"****第三级****3"**,
             **"children"**:**[]**
           },
           {
             **"title"**:**"****第三级****4"**,
             **"children"**:**[]**
           }
         ]
       }
     ]
   }
 ]

看了上图大家可能有疑问,JSON树体现了各级的元素,那元素数组在树的哪里体现出来呢?其实很简单,上图中的第一级、第二级的children后中括号[]就是表示设有元素数组,如果少了[],说明下级元素直接添加给了上级元素,json验证也会通过,但个人觉得不够规范。

接下来,再进一步的想法就是中间层根据数据表生成json树再传回小程序,中间层接口代码如下:

例子中的pbtz表的operator,linename,cardname三个字段分别代表父、子、孙三个节点的值

代码语言:javascript
复制
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

小程序接收后显示的树型结构如下图。

小程序接收显示我是参考网上大侠的代码:

https://blog.csdn.net/weixin_44646763/article/details/122751392

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-08-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 加菲猫的VFP 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、不管多少级,每一级都要有元素和元素数组。
  • 二、赋值顺序为:下一级元素--->下一级元素数组--->上一级元素--->上一级元素数组
  • 三、赋值采取“吃吐大法”(我觉得用吃吐比较形象)。
相关产品与服务
云开发 CloudBase
云开发(Tencent CloudBase,TCB)是腾讯云提供的云原生一体化开发环境和工具平台,为200万+企业和开发者提供高可用、自动弹性扩缩的后端云服务,可用于云端一体化开发多种端应用(小程序、公众号、Web 应用等),避免了应用开发过程中繁琐的服务器搭建及运维,开发者可以专注于业务逻辑的实现,开发门槛更低,效率更高。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档