如何从php中读取json对象

时间:2015-08-28 12:18:33

标签: php android mysql json

我是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); ?>

我的输出为

[[],{&#34;名称&#34;:&#34; $名称&#34;&#34;密码&#34;:&#34; $密码&#34;,& #34;电子邮件&#34;:&#34; $电子邮件&#34;&#34;地址&#34;:&#34; $地址&#34;},{&#34;名称&#34;:&# 34;如是&#34;&#34;密码&#34;:&#34; dfsdf&#34;&#34;电子邮件&#34;:&#34; dsfgs&#34;&#34;地址&#34 ;:&#34; ssfsfsdf&#34;},{&#34;名称&#34;:&#34; HMD&#34;&#34;密码&#34;:&#34; 123&#34;,& #34;电子邮件&#34;:&#34; RDF&#34;&#34;地址&#34;:&#34; sfdssf&#34;},{&#34;名称&#34;:&#34; SDFS&#34;&#34;密码&#34;:&#34; sdfsf&#34;&#34;电子邮件&#34;:&#34; sdfsdf&#34;&#34;地址&#34 ;: &#34; SDF&#34;},{&#34;名称&#34;:&#34;如是&#34;&#34;密码&#34;:&#34; sadsa&#34;&#34 ;电子邮件&#34;:&#34; xdfsaf&#34;&#34;地址&#34;:&#34; sdfsdf&#34;}]

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

  •  0
  •   
  • 1
  •    
  • 2
  •  
  • 3
  •  
  • 4
  •  
  • 5
  • 如何在我的Android应用程序中阅读json对象中的详细信息&#39; 0&#39;,#1;#...,... ...

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

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

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

    2 个答案:

    答案 0 :(得分:0)

    使用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..
    }
    

    答案 1 :(得分:0)

    你应该尝试一下,

    <?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
    

    希望这能解决您的问题。如果它不是

    ,请告诉我
    相关问题