简体   繁体   English

在PHP中将字符串转换为JSON对象或数组

[英]Converting string to json object or array in php

I have to make a web service. 我必须做一个网络服务。 Therefore I referred some tutorials in the internet and came up with the following codes 因此,我参考了互联网上的一些教程,并提出了以下代码

index.php index.php

<html>
<head>
    <title>Form page</title>
</head>

<body>

    <form action="http://localhost:81/my%20web%20service/webservice" method="get">
        Table name:<br>
        <input type="text" name="s" value=""><br>
        <input type="submit" value="Submit">
    </form>
</body>
</html>

webservice.php webservice.php

<?php

    include('connectdb.php');

    $something = $_GET['s'];
    $sqlcode = mysql_query("Select * from $something");

    $jsonObj= array();
    while($result=mysql_fetch_object($sqlcode))
    {
        $jsonObj[] = $result;
    }

    $final_res =json_encode($jsonObj) ;
    echo $final_res;
?>

connectdb.php connectdb.php

<?php
    $hostname="localhost";
    $username="root"; //write your username
    $password=""; //write your password
    $db_name="webservice_trial"; //write your db name
    $con=mysql_connect($hostname,$username,$password);
    mysql_select_db($db_name,$con) or die ("Cannot connect the Database");
    mysql_query("SET NAMES 'utf8'",$con); 

?>

The above codes works fine. 上面的代码工作正常。 When I enter a name of a table from form.php, it will retrieve all tuples in that particular table. 当我从form.php输入表名时,它将检索该特定表中的所有元组。

Now what I want to do is to make data display on another page. 现在,我要做的是使数据显示在另一页上。 ie I want to transfer data from webservice.php to another page from json format. 即我想将数据从webservice.php传输到json格式的另一页。 so I edited my webservice.php as following 所以我如下编辑了我的webservice.php

webservice.php webservice.php

<?php

    include('connectdb.php');

    $something = $_GET['s'];
    $sqlcode = mysql_query("Select * from $something");

    $jsonObj= array();
    while($result=mysql_fetch_object($sqlcode))
    {
        $jsonObj[] = $result;
    }

    $final_res =json_encode($jsonObj) ;
    echo $final_res;

    $jsonArray = (array) json_decode($final_res);
    echo $jsonArray[0];

?>

it gives the following error 它给出了以下错误

[{"name":"hilton","town":"colombo","telephone":"774933705","description":"excellent"},{"name":"galadari","town":"colombo","telephone":"112894143","description":"best"},{"name":"mt. lavinia","town":"mt. lavinia","telephone":"773580324","description":"good"}]

Catchable fatal error: Object of class stdClass could not be converted to string in C:\\xampp\\htdocs\\json_folder\\my web service\\webservice.php on line 18 可捕获的致命错误:第18行的C:\\ xampp \\ htdocs \\ json_folder \\ my web service \\ webservice.php中无法将类stdClass的对象转换为字符串

You can either change 你可以改变

$jsonArray = json_decode($final_res);

to

$jsonArray = json_decode($final_res, True);  

or, to access the name of the first item use 或者,要访问第一项的名称,请使用

print $jsonArray[0]->name;

Instead of trying to typecast the decoded string to an array, use this, which is the right way to do it. 与其尝试将解码后的字符串类型转换为数组,不如使用它,这是正确的方法。

$jsonArray = json_decode($final_res, TRUE);

Now, you will have an array, which you can echo or var_dump . 现在,您将拥有一个数组,可以echovar_dump

var_dump($jsonArray);

As in your question, what your error says is that, you are trying to echo it as a variable, where what you are trying to echo is actually an object class. 就像在您的问题中一样,您的错误说明的是,您试图将其作为变量进行echo ,而您试图回显的实际上是对象类。 Probably because it isnt being typecasted. 可能是因为它没有被强制转换。

Advices: 忠告:

  • Please stop using mysql functions. 请停止使用mysql函数。 Its deprecated. 不推荐使用。 Use mysqli or PDO . 使用mysqliPDO

  • Read about SQL Injections. 了解有关SQL注入的信息。 Never trust userinputs. 永远不要相信用户输入。 Always sanitize them or use prepared statements. 始终对其进行消毒或使用准备好的语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM