PB 生成和解析JSON格式数据

本文探讨了如何利用Protocol Buffers(PB)生成和解析JSON格式数据,详细介绍了PB的优势及在前端JavaScript中的应用,旨在提升数据处理效率和降低网络传输成本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一 创建用户对象sailjson

forward
global type sailjson from nonvisualobject
end type
type json_pair from structure within sailjson
end type
end forward

type json_pair from structure
	string		name
	any		value
end type

global type sailjson from nonvisualobject
end type
global sailjson sailjson

type variables
/*
	Sailjson:from www.pblsoft.com
	Please reserve this information
	
	Version:2.1
	Release date:2016-1-13
	update:add root is array. getrootarray
	
	Version:2.0
	Release date:2016-1-1
*/

private:
	json_pair pairs[]
	string is_json
	char ichars[]
	long idx, imaxlen

	treeview tree
	integer objectpcxidx, arraypcxidx, itempcxidx
	string is_ident
public:
	boolean ignoreCase = false
	integer ilevel
	string pair_index

end variables

forward prototypes
public function string parse (string as_json)
public function integer getarray (string itemname, ref any va[])
public function boolean isarray (any value)
public function any getattribute (string itemname)
private function integer of_readobject (sailjson vjson, integer alevel)
private subroutine of_error (string msg)
private subroutine skipspc ()
private function long findpairindex (string as_name)
private function string of_getformat (sailjson ajson, string ident)
private function string mymid (character vchars[], long vstart, long vlen)
public subroutine buildtree (treeview atree, long handle, integer aobjectpcxidx, integer aarraypcxidx, integer aitempcxidx)
private subroutine of_buildtree (sailjson ajson, long handle)
public subroutine setattribute (string as_name, any aa_value)
public function any addarrayitem (string arrayname)
public function any addobject (string objname)
public function string getformatjson (string ident)
private function integer of_readarray (ref any varray, integer alevel)
private subroutine of_buildtreeofarray (any varray, long handle)
private function string of_getformatofarray (any varray, string ident)
public function integer getrootarray (ref any va[])
end prototypes

public function string parse (string as_json);json_pair pairsnull[]
pairs = pairsnull

is_json = as_json
ichars = as_json
imaxlen = len(as_json)
integer li_level = 0

idx = 0
skipspc()
if ichars[idx] = '{' then
	of_readobject(this, li_level)
elseif ichars[idx] = '[' then
	any la
	of_readarray(la, li_level)
	pairs[1].value = la
	//pairs[1].name = "root"
	this.pair_index += ";root=1"
end if


return ''
end function

public function integer getarray (string itemname, ref any va[]);any la

la = GetAttribute(itemname)

if isArray(la) then
	va= la
	return upperbound(va)
end if
return 0
end function

public function boolean isarray (any value);return classname(value)='any'
end function

public function any getattribute (string itemname);long index
if ignoreCase then
	itemname = upper(itemname)
end if
index = findpairindex(itemname)
if index = 0 then
	SignalError(42,"JSON.getAttribute: attribute "+string(itemname,"[general]")+" does not exists!")
	//return "!ERROR!"
	return ""
end if
return pairs[index].value
end function

private function integer of_readobject (sailjson vjson, integer alevel);//{....}
integer li_level , pairidx, arrayidx
string ls_name, ls_value
any la_value, la[], lanull[]
sailjson ljson

if ichars[idx] <> '{' then
	return -1
end if
li_level = alevel + 1
vjson.ilevel = li_level
skipspc()
do while idx < imaxlen
	//read key name, the first char is "
	if ichars[idx] = '"' then
		idx ++
		ls_name = ''
		ls_value = ''
		do while ichars[idx] <> '"'
			ls_name = ls_name + ichars[idx]
			idx++
		loop
		ski
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值