dpy15285 2015-08-28 12:18
浏览 69

如何从php中读取json对象

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

  • 写回答

2条回答 默认 最新

  • dongqiang2358 2015-08-28 12:36
    关注

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

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助