使用gson解析jsonarray和jsonobject

时间:2015-07-25 04:35:25

标签: arrays gson jsonobject

成功返回时的登录API:

{"status":0,"data":{userid:1,username:"abc"},"msg":"login sucess"}

但是当错误服务器返回时:

{"status":0,"data":[],"msg":"login failure!"}

如何定义对象,然后使用gson解析数据字段! 感谢。

1 个答案:

答案 0 :(得分:0)

json 还是 gson ?例如,您的回复是针对此变量$response;

您可以尝试:

<?php
$response = '{"status":0,"data":{userid:1,username:"abc"},"msg":"login sucess"}';

// $response = '{"status":0,"data":[],"msg":"login failure!"}';

$response = json_decode($response);
if(empty($response->data){
    echo $response->msg;
}

// print_r($response); //you can try to print_r $response here
// if have data to login, do what you want