android的json,Android中自定义通用Json解释器

通用Json解释器 返回Json格式为: {"status":0,"message":"文本信息","data":[]}

package com.suniot.caigou.parser;

import java.lang.reflect.Type;

import org.apache.http.util.TextUtils;

import org.json.JSONException;

import org.json.JSONObject;

import com.google.gson.Gson;

import com.lyh.lib.utils.DebugLog;

import com.suniot.caigou.entity.JsonResult;

import com.suniot.caigou.myenum.JsonStatus;

/**

* @author lyh

* @description 通用Json解释器 返回Json格式为: {"status":0,"message":"文本信息","data":[]}

* @date 2014-11-16

* @param

*/

public class JsonParser {

private Class classOfT;

private Type typeOfT;

public JsonParser(Class classOfT) {

this.classOfT = classOfT;

}

public JsonParser(Type typeOfT) {

this.typeOfT = typeOfT;

}

/**

* JSON字符串转实体类

*

* @param jsonStr

* @return

* @throws JSONException

*/

public JsonResult parseJSON(String jsonStr) throws JSONException {

JsonResult result = new JsonResult();

if (TextUtils.isEmpty(jsonStr)) {

result.setStatus(JsonStatus.FAIL);

result.setMessage("服务器未返回数据");

return result;

}

JSONObject jsonObject = null;

try {

jsonObject = new JSONObject(jsonStr);

} catch (Exception e) {

DebugLog.e("服务器返回数据格式错误");

DebugLog.e(e.getLocalizedMessage());

result.setStatus(JsonStatus.FAIL);

result.setMessage("服务器返回数据格式错误");

return result;

}

try {

int status = jsonObject.getInt("status");

result.setStatus(JsonStatus.valueOf(status));

} catch (Exception e) {

DebugLog.e("缺少staus的值或status的值类型错误(必须为Int型)");

DebugLog.e(e.getLocalizedMessage());

result.setStatus(JsonStatus.FAIL);

result.setMessage("服务器返回数据格式错误");

return result;

}

try {

String message = jsonObject.getString("message");

result.setMessage(message);

} catch (Exception e) {

DebugLog.e(e.getLocalizedMessage());

}

try {

String data = jsonObject.getString("data");

if (!TextUtils.isEmpty(data) && !data.equals("[]")

&& !data.equals("{}")) {

T entity = null;

if (classOfT != null)

entity = (T) new Gson().fromJson(data, classOfT);

else if (typeOfT != null)

entity = (T) new Gson().fromJson(data, typeOfT);

result.setData(entity);

}

} catch (Exception e) {

DebugLog.e(e.getLocalizedMessage());

}

return result;

}

}

package com.suniot.caigou.myenum;

/**

* @author lyh

* @description 服务器返回的状态

* @date 2015-04-15

*/

public enum JsonStatus {

/**

* 未登录

*/

NOT_LOGIN(-1),

/**

* 失败

*/

FAIL(0),

/**

* 成功

*/

SUCCESS(1);

private int value = 0;

private JsonStatus(int value) {

this.setValue(value);

}

public static JsonStatus valueOf(int value) {

switch (value) {

case -1:

return NOT_LOGIN;

case 0:

return FAIL;

case 1:

return SUCCESS;

default:

return FAIL;

}

}

public int getValue() {

return value;

}

public void setValue(int value) {

this.value = value;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值