繁体   English   中英

如何从php读取json对象

[英]how to read json object from php

我是json的新手,我正在尝试从MySQL数据库读取值并将结果编码为json数据。为此,我编写了以下代码

<?php
$conn=mysql_connect('localhost','root','');
$mydb=mysql_select_db("json",$conn) or die('failed');
$result=mysql_query("select * from register",$conn);
$rowsarray[]=array();
$rowsarra[]=null;
while($rows=mysql_fetch_assoc($result)){
$rowsarray[]=$rows;}
echo  json_encode($rowsarray); ?>

我正在作为输出

[[],{“ name”:“ $ name”,“ password”:“ $ password”,“ email”:“ $ email”,“ address”:“ $ address”},{“ name”:“ rushi” ,“ password”:“ dfsdf”,“ email”:“ dsfgs”,“ address”:“ ssfsfsdf”},{“ name”:“ hmd”,“ password”:“ 123”,“ email”:“ rdf” ,“地址”:“ sfdssf”},{“名称”:“ sdfs”,“密码”:“ sdfsf”,“电子邮件”:“ sdfsdf”,“地址”:“ sdf”},{“名称”:“ rushi“,” password“:” sadsa“,” email“:” xdfsaf“,” address“:” sdfsdf“}]

当我在在线jsonviewer中查看此内容时,我得到的输出为+ JSON

  • +0
  • +1
  • +2
  • +3
  • +4
  • +5
  • 我如何在android应用程序中读取json对象中'0','1',...的详细信息

    JsonArray jsonarray=new JsonArray(response.tostring);
    JsonObject job=jsonarray.getstring("0");
    

    这是获取jsonobject的方法吗? (要么)

    我怎样才能给员工电子邮件代替0,1,2,...请帮助我。 如果我在任何地方都不对,请纠正我。 提前致谢

    使用jsonlint可以清楚地了解格式。 将结果复制并粘贴到jsonlint中,然后像这样进行验证。

    [
    [],
    {
        "name": "$name",
        "password": "$password",
        "email": "$email",
        "address": "$address"
    },
    {
        "name": "rushi",
        "password": "dfsdf",
        "email": "dsfgs",
        "address": "ssfsfsdf"
    },
    {
        "name": "hmd",
        "password": "123",
        "email": "rdf",
        "address": "sfdssf"
    },
    {
        "name": "sdfs",
        "password": "sdfsf",
        "email": "sdfsdf",
        "address": "sdf"
    },
    {
        "name": "rushi",
        "password": "sadsa",
        "email": "xdfsaf",
        "address": "sdfsdf"
    }
    ]
    

    根据该数据结果,您只需编写以下代码。

    JSONArray resultArray = new JSONArray(your result);
    int length = resultArray.length();
    for (int i = 0; i < length; i++) {
        JSONObject jsonItems = jsonFaovourites.getJSONObject(i);
        String name = jsonItems.getString("name");
        String email= jsonItems.getString("email");
        //password etc..
    }
    

    你应该试试看

    <?php
    header("Access-Control-Allow-Origin: *");
    header("Content-Type: application/json; charset=UTF-8");
    
    $conn = new mysqli("localhost", "root", "myPassword", "json");
    
    
    $result = $conn->query("SELECT * FROM register");
    
    $outp = "";
    while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
        if ($outp != "") {$outp .= ",";}
        $outp .= '{"field1":"'  . $rs["field1"] . '",';
        $outp .= '"field2":"'   . $rs["field2"]        . '",';
        $outp .= '"fieldx":"'. $rs["fieldx"]     . '"}'; 
    
        //fieldx where x is the next column/filed if any and filed the name of the column the exact way it is on the database  
    }
    $outp ='{"records":['.$outp.']}';
    $conn->close();
    
    echo($outp);
    
    //Note this returns json to the browser so no need to json_encode
    ?>
    

    现在,您可以将其作为普通的JSON对象读取,而无需进行编码。 但是要读取来自您的android应用程序的JSON字符串,这将是另一种方法

    你会做类似的事情

    $data //data from android application 
    
    $data = json_decode($data);
    
    $data->field //to access the field/property in this object
    

    希望这能解决您的问题。 让我知道是否可以

    暂无
    暂无

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

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