【问题标题】:Using gson to parse jsonarray and jsonobject使用gson解析jsonarray和jsonobject
【发布时间】:2015-07-25 04:35:24
【问题描述】:

成功返回时的登录api:

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

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

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

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

【问题讨论】:

  • 成功时状态码为1,错误时状态码为0;

标签: arrays gson jsonobject


【解决方案1】:

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

【讨论】:

  • 使用java语言和json lib google gson进行解析
  • 这样您就可以使用 fromJson(response); 调用响应获取数据
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-12
  • 1970-01-01
  • 2012-11-06
相关资源
最近更新 更多