简体   繁体   中英

how to read json object from php

I am newbie to json.I am trying to read values from MySQL database and encode the result to json data.for that i have written following coding

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

i am getting output as

[[],{"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"}]

when i view this in online jsonviewer i get the output as +JSON

  • +0
  • +1
  • +2
  • +3
  • +4
  • +5
  • how can i read the details inside the json object for '0','1',... in my android application

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

    is this the way to get the jsonobject? (or)

    how can i give employee email in place of 0,1,2,... please help me out. if i am wrong anywhere please correct me. Thanks in advance

    Use jsonlint for clear understanding about the format. Copy and paste your result in jsonlint and then just validate it like this.

    [
    [],
    {
        "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"
    }
    ]
    

    From that result of data you just write the below code.

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

    You should try this out,

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

    You can now read this as a normal JSON object, no need to encode. But to read a JSON string coming from your android application that would be a different approach

    You will do something like

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

    Hope this solves your problem. Let me know if it doesn't

    The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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