在服务器端处理JSON [英] Handling JSON on server-side

查看:216
本文介绍了在服务器端处理JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的Andr​​oid应用程序将数据发送到我的服务器。
在客户端,这意味着应用程序,我可以创建JSON对象并将其发送给服务器。
问题是,我不知道如何处理在服务器上side.All我想我的服务器做的是接受JSON,解析,并展示给me.That的吧。

I want to send data from my Android app to my server. On the client-side, meaning the application,I can create the JSON object and send it to the server. The problem is, I don't know how to 'handle' it on the server side.All I want my server to do is to receive the JSON, parse it, and show it to me.That's it.

我知道这是pretty模糊的问题,但我真的不知道在这里启动,并会喜欢,如果有人能告诉我一个完整的教程。

I know it's pretty vague question, but I don't really know where to start here, and would love if anyone could show me a complete tutorial.

谢谢!

推荐答案

使用PHP和json_de code()
http://php.net/manual/en/function.json -de code.php

Use PHP and json_decode() http://php.net/manual/en/function.json-decode.php

下面一个简单的例子如何处理数据:

Here a quick example how to handle the data:

        // get json
        $input = json_decode($_GET["json"]);

        // get values
        $firstname = $input->firstName;
        $surename = $input->lastName;
        $age = intval($input->age);

        // check values
        if (isset($firstname) && !empty($firstname) && 
            isset($surename) && !empty($surename) &&
            isset($age) && is_numeric($age))
        {
            // do something
            echo "Hello ".htmlspecialchars($firstname)." ".htmlspecialchars($surename)."!<br>";
            echo "You are $age years old! Wow.";
        }
        else
        {
            echo "Some values are missing or incorrect";
        }

我用GET参数在这个例子中。如果你有更大的数据,使用POST而不是GET。

I used the GET parameter in this example. If you have larger data, use POST instead of GET.

例如:
网址: HTTP://localhost/test/index.php JSON = {的名字 :约翰,姓氏:李四,时代:23}
输出:John Doe:您好!
你是23岁!哇。

Example: URL: http://localhost/test/index.php?json={ "firstName" : "John","lastName" : "Doe","age" : 23 } Output: Hello John Doe! You are 23 years old! Wow.

但:请确保您连接code。在应用程序中的JSON数据。在我的例子浏览器做的。

But: Make sure you encode the JSON data at your application. In my example the browser does it.

这篇关于在服务器端处理JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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