【问题标题】:Php JSONArray decoding and performing Database Operationphp JSONArray 解码并执行数据库操作
【发布时间】:2016-04-23 16:48:59
【问题描述】:

我正在从我的 android 应用程序中发送一个 JsonArray,如下所示:

   final JSONArray data = new JSONArray();

    try{
        for(int i = 0; i<contactsList.size(); i++){
            JSONObject jobj = new JSONObject();
            ObjectContacts ob = contactsList.get(i);
            jobj.put("contactid", ob.getContact_id());
            jobj.put("mobile", ob.getNumber());
            data.put(jobj);
        }

    } 

以及我的服务器收到的结果数组。

[
    {"contactid":"3","mobile":"(545) 454-5445"},
    {"contactid":"1","mobile":"(880) 939-5212"},
    {"contactid":"2","mobile":"822895456165"}
]

我需要从这个数组中获取手机号码并执行 Db 操作来查看这个手机号码是否存在。如何访问每个手机并执行查询?查询将包括查找手机号码是否存在,如果为真,它将获取属于手机号码的姓名,最后以 JSONArray 格式将数组返回给移动应用程序,其中包括联系人 ID、手机、状态(是或否),姓名。

时间不是问题,但有时数组可能包含 300-400 个手机号码,具体取决于用户的联系方式。

更新

这是我实现的新 PHP 代码:

$app->post('/getcontacts', function () use ($app) {

//Verifying the required parameters
verifyRequiredParams(array('data'));

//Creating a response array
$response = array();


//reading post parameters
$data = $app->request->post('data');

$data_array = json_decode($data);


foreach ( $data_array as $obj ) {

 $res = array();
   $db = new DbOperation();
   $r = $db->checkContactExist($obj->mobile);
   if($r){
      $res["contactid"] = $obj->contactid;
      $res["mobile"] = $obj->mobile;
      $res["name"] = $obj->name;
      $res["status"] = 'yes';
      $res["image_small"] = $db->getImageSmall($obj->mobile);
   } else {
      $res["contactid"] = $obj->contactid;
      $res["mobile"] = $obj->mobile;
      $res["name"] = $obj->name;
      $res["status"] = 'no';
      $res["image_small"] = '';
   }

}

$response["error"] = false;
$response["message"] = json_encode($res);
echoResponse(201, $response);

});

我从服务器得到的响应:

{"contactid":"3","mobile":"(943) 101-9713","status":"no","image_small":""}

虽然应该有三个联系人,但它只能读取一个。

如果我只是通过 echo 将传入数据发送回应用程序以检查是否所有联系人都来了,那么它工作正常。也许在循环中我只添加了一个联系人的详细信息。

第二次更新

已经解决了这个问题:

$final_res = array();

在foreach循环中

$final_res[] = $res;

因此发回:

json_encode($final_res);

【问题讨论】:

  • 我尝试循环,然后获取手机号码并执行数据库查询。但循环只运行一次,因为 count(JSONArray) 只显示 1

标签: php android mysql mobile


【解决方案1】:

在字符串上使用json_decode() 后,该字符串将转换为 PHP 对象数组。

这就是你处理它的方式

<?php
$json_string = '[
    {"contactid":"3","mobile":"(545) 454-5445"},
    {"contactid":"1","mobile":"(880) 939-5212"},
    {"contactid":"2","mobile":"822895456165"}
]';

$json_array = json_decode($json_string);

foreach ( $json_array as $obj ) {
   echo $obj->contactid . ' - ' . $obj->mobile . PHP_EOL;
}

结果:

3 - (545) 454-5445
1 - (880) 939-5212
2 - 822895456165

您应该能够接受并围绕这个简单的代码添加任何数据库访问权限

【讨论】:

  • 什么。 PHP_EOL 在这里?
  • 我使用 PHP CLI 对此进行了测试,如果您使用浏览器进行输出,则可以将其替换为 &lt;br&gt;。 PHP_EOL 是一个 PHP 常量,它包含一个符合 Windows 或 Unix 系统的 NewLibe 字符,并为这些选项中的任何一个提供正确的字符
  • 嗨。我尝试实现您的方式,但无法访问数组中的所有行。这是 php 代码 $response = array(); $final_res = 数组(); //读取post参数 $data = $app->request->post('data'); $data_array = json_decode($data);
  • foreach ( $data_array as $obj ) { $res = array(); $db = 新的 DbOperation(); $r = $db->checkContactExist($obj->mobile); if($r){ $res["contactid"] = $obj->contactid; $res["mobile"] = $obj->mobile; $res["status"] = '是'; $res["image_small"] = $db->getImageLarge($obj->mobile); } else { $res["contactid"] = $obj->contactid; $res["mobile"] = $obj->mobile; $res[“状态”] = '否'; $res["image_small"] = ''; } $final_res["数据"] = $res; }
  • $response["error"] = false; $response["message"] = json_encode($final_res); echoResponse(201, $response);
猜你喜欢
  • 2012-01-09
  • 2014-11-14
  • 2017-03-19
  • 2018-03-31
  • 1970-01-01
  • 2012-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多