从POST数据在php中构建一个json [英] Build a json in php from POST data

查看:120
本文介绍了从POST数据在php中构建一个json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过url发布数据更新对象的json列表.例如,使用url:

I need to update a json list of object via url post data. For example, with url:

http://myweb.com/index.php?name=Peter&surname=Brown

在php中,使用get方法:

in php, using get method:

$name = $_GET["name"];
$surname = $_GET["surname"];
$json = array();

        $json["nombre"] = $name;
        $json["lat"] = $lat;
        $data[] = $json;

$json_end= json_encode($data);

和json_end有效地完成了,就像我想要的:

and json_end efectively is done like I want:

[{"name":"Peter","surname":"Brown"}]

我的问题是关于如何才能增加json,以构建像这样的数组:

My question is about how I can do it incrementing the json, in order to build an array like:

[{"name":"Peter","surname":"Brown"}]
[{"name":"newname","surname":"newsurname"}]
// and so on

每次用户使用带有新参数的网址.

each time user use the url with new parameters.

我需要写文件还是数据库?任何提示都将不胜感激.

Do I need to write to a file, or to database? Any tips will be apreciated.

这个想法是希望任何用户都可以使用url添加一些数据.我试图将json存储到一个领域,但存储仅在当前会话期间是持久的.

The idea is to be able that any user can add some dat using url. I tried to store the json to a fiel but the storing is durable only along the current session.

推荐答案

<?
/* This needs to be at the top of your file, without ANYTHING above it */
session_start();

/* ... */

if(!array_key_exists('entries', $_SESSION))
{
    $_SESSION['entries'] = array();
}

$_SESSION['entries'][] = array("name" => $_GET["name"], "surname" => $_GET["surname"]);

$json_string = json_encode($_SESSION['entries']);

这将产生一个JSON.但是我不知道您是否想要,但是您的输出是一系列单独的JSON.您可以通过将上面的最后一行替换为:

This would produce a single JSON. However I don't know whether you meant to or not, but your output is a series of separate JSONs. You could do this by replacing the last line above with:

$json_string = '';
foreach($_SESSION['entries'] as $entry){ 
    $json_string.= json_encode($entry) . "\n";
}

您可能还希望有一种方法来重置/清空阵列.在这种情况下,我将从以下位置更改if测试:

You would also probably want a way to reset/empty the array. In which case I'd change the if test from:

if(!array_key_exists('entries', $_SESSION))

收件人:

if(!array_key_exists('entries', $_SESSION) || array_key_exists('reset', $_GET))

您可以通过访问哪些方式使用

Which you could use by visiting

http://myweb.com/index.php?reset


如果在某处添加以下代码:


If somewhere you add the following code:

foreach($_SESSION['entries'] as $id=>$entry){ 
    printf("%2d: %s\n", $id, json_encode($entry));
}

您将获得按其各自的键枚举的json元素的列表.例如,它可能看起来像:

You'll get a list of the json elements enumerated by their respective keys. For example, it might look like:


 0: "[{\"name\":\"Peter\",\"surname\":\"Brown\"}]"
 1: "[{\"name\":\"newname\",\"surname\":\"newsurname\"}]"
 2: "[{\"name\":\"George\",\"surname\":\"Washington\"}]"
 3: "[{\"name\":\"John\",\"surname\":\"Adams\"}]"

如果您随后添加以下代码:

If you then add the following code:

if(array_key_exists('del', $_GET) && is_numeric($_GET['del']))
{
    $key = (int)$_GET['del'];
    if(array_key_exists($key, $_SESSION['entries']))
    {
        unset($_SESSION['entries'][$key]);
    }
    else
    {
        printf('<strong>ERROR: $_GET['del'] = %d but $_SESSION['entries'][%d] doesn't exist.</strong>', $key, $key);
    }
}

通过将id指定为del GET参数,您将能够删除各个json条目.

you'll be able to delete individual json entries by specifying id as the del GET parameter.

例如,

http://myweb.com/index.php?del=2

将删除与'[{"name":"George","surname":"Washington"}]';

其余条目为:


 0: "[{\"name\":\"Peter\",\"surname\":\"Brown\"}]"
 1: "[{\"name\":\"newname\",\"surname\":\"newsurname\"}]"
 3: "[{\"name\":\"John\",\"surname\":\"Adams\"}]"
 4: "[{\"name\":\"Thomas\",\"surname\":\"Jefferson\"}]"

这篇关于从POST数据在php中构建一个json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆