douren9077 2016-04-23 16:48
浏览 34
已采纳

Php JSONArray解码并执行数据库操作

I'm sending a JsonArray from my android application as this:

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

    } 

And the resulting Array that my server receive.

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

I need to fetch mobile numbers from this array and perform a Db Operation to look if this mobile number exist or not. How can I access each mobile and perform a Query? The query will consist of looking for mobile number existence and if it's true, it will fetch the name belonging to the mobile number and finally return an array back to the mobile application in JSONArray format which will consist of contact id, mobile, status(Yes or No), name.

Time won't be an issue but sometimes the array may contain 300-400 mobile number depending on user's contact.

Update

Here's the new Php Code that I implemented:

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

});

The response I get from server:

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

While there should be three contacts, it could only read one.

If I just send the incoming data back to application through echo to check whether all the contacts is coming or not, then it works correct. Maybe in the loop I'm adding detail about only one contact.

Second Update

Have solved the issue by putting :

$final_res = array();

and in foreach loop

$final_res[] = $res;

thus sending this back:

json_encode($final_res);
  • 写回答

1条回答 默认 最新

  • draw62188 2016-04-23 17:11
    关注

    That string will convert to a PHP array of objects, after using json_decode() on the string.

    So this is how you process it

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

    Result:

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

    You should be able to take this and add any database access around this simple code

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)