search
Homephp教程php手册php 无限级数据JSON格式及JS解析

php 无限级数据JSON格式及JS解析

Jun 13, 2016 pm 12:14 PM
flashjsonphpcodedataunlimitedFormatofparseneed

Flash需要JSON格式的数据,于是,就有了如下代码:(PHP实现,C#类似,JSON库自己去下吧~~)

复制代码 代码如下:


//--查询用户下线信息,返回JSON,用于flash
if(!empty($_GET['action'])&&!empty($_GET['invite'])){
//固定参数请求才会返回信息
if($_GET['action']=='getinfo'&&$_GET['invite']==1){
//数据实体,一个实体类
class UcInvite{
//---显示用的数据信息
public $fuid; //用户id
public $funame; //用户名
public $furl; //用户空间地址
//---显示用的数据信息
public $fchilds; //子类集合
}
$invitecount = 0; //总数量,用于记录全部下线数量
//递归实现方法
function GetShowTreeInvite($uid){
global $_SGLOBAL,$invitecount;
//组织sql语句并查询,表中fuid是下线的id,uid是父id,fusername是用户名字
$inv_sql = "select fuid,fusername from ".tname("invite")." where uid = ".$uid;
$inv_query = $_SGLOBAL['db']->query($inv_sql);
//下表及返回数组
$index = 0;
$inviteTree = Array();
$invitezcount = 0; //每个子类下的数量
//循环添加信息到数组
while($v = $_SGLOBAL['db']->fetch_array($inv_query)){
$ui = new UcInvite();
$ui->fuid = $v['fuid'];
$ui->funame = $v['fusername'];
$ui->furl = "/home/space.php?uid=".$v['fuid'];
//调用自己,递归查询子类信息
$ui->fchilds=GetShowTreeInvite($v['fuid']);
//插入返回的数组中
$inviteTree[$index]=$ui;
$index++;
$invitecount++;
$invitezcount++;
}
//记录当前下线的下线数量
$inviteTree['invitezcount'] = $invitezcount;
//返回数组信息
return $inviteTree;
}
$fuid = empty($_GET['fuid'])?$space[uid]:$_GET['fuid'];
//根据当前登录用户ID查询信息,并返回一个集合
$inviteTree = GetShowTreeInvite($fuid);
//记录总下线数
$inviteTree['invitecount'] = $invitecount;
//引入json库,这里用的是Services_JSON
//因为并不确定服务器都是PHP5.2以上版本,所以没有用自带的JSON函数
require_once('../plugins/JSON/JSON.php');
//json输出
$json = new Services_JSON();
echo $json->encode($inviteTree);
exit;
}else{
echo '请求参数错误!';
exit;
}
}
//--查询用户下线信息,返回JSON,用于flash


以上代码返回一个JSON字符串,如:
{'0':{'fuid':'950','funame':'郭梓原','furl':'/home/space.php?uid=950','fchilds':{'invitezcount':0}},'invitezcount':1,'invitecount':1}
这里只查询了一个
接下来用JS进行以下解析:

复制代码 代码如下:





这样,两个无限级的操作就完成了,结果截图:

珍惜劳动成果,虽说内容不多,可是也是一个一个字打上的,转载请注明!!关注爱拼可乐吧

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)