使用方法,冒号代表数组
root = jsonParse(~"{\"action\":\"test\",\"data\":{\"token\":\"token\",\"ff\":0.1234,\"aar\":[1,2,3,4,5]}}")
jsonGetInteger(root,"data.aar:1")
EnableExplicit
Procedure jsonGetIetem(jv,path.s,m.s="")
Define dp,zp,dg.s,node.s
If jv=0
ProcedureReturn 0
EndIf
dp = FindString(path,".")
zp = FindString(path,":")
dg="."
If (dp>zp And dp>0) Or (dp=0 And zp>dp)
dp=zp
dg=":"
EndIf
If dp =0
node = path
If m=":"
ProcedureReturn GetJSONElement(jv,ValD(node))
Else
ProcedureReturn GetJSONMember(jv,node)
EndIf
Else
node=Left(path,dp-1)
path = Right(path,Len(path)-dp)
If m=":"
ProcedureReturn jsonGetIetem(GetJSONElement(jv,ValD(node)),path,dg)
Else
ProcedureReturn jsonGetIetem(GetJSONMember(jv,node),path,dg)
EndIf
EndIf
EndProcedure
Procedure jsonParse(str.s)
Define jid = ParseJSON(#PB_Any,str)
If jid<>0
ProcedureReturn JSONValue(jid)
EndIf
ProcedureReturn 0
EndProcedure
Procedure.s jsonGetString(jv,path.s)
Define jr = jsonGetIetem(jv,path)
If jr<>0 And JSONType(jr)=#PB_JSON_String
ProcedureReturn GetJSONString(jr)
Else
ProcedureReturn ""
EndIf
EndProcedure
Procedure jsonGetInteger(jv,path.s)
Define jr = jsonGetIetem(jv,path)
If jr<>0 And JSONType(jr)=#PB_JSON_Number
ProcedureReturn GetJSONInteger(jr)
Else
ProcedureReturn 0
EndIf
EndProcedure
本站文章除注明转载/出处外,均为博主 spooking 原创或翻译,转载前请务必署名。