帮助我,伙计们! 请建议一个简单的解决方案,在Android中将类型boolean从server转换为int :) 当我登录时,我会从服务器中得到这样的响应:
{"status":{"error":0,"code":200,"message":"OK"},"response":{"profile":{"id":114,"username":"k@gmail.com","full_name":"k","phone":"9999999","verified":1,"admin":0,"allow_dev":false,"company":{"id":9,"name":"ООО \"Фингерз медиа\"","email":"info@fingers.by","sample":null,"logo":"http://storage.guardian-glass.fingersmedia.by/0cb56968b3cec1bba301db8d51d1015e.jpg"}},"access_token":"15629e234e04a54a5a44ef2aa4eccb1d"}}
然后我得到undefined exception: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected NUMBER but was BOOLEAN
这是因为JsonElement“ allow_dev ”是来自服务器的 boolean ,而在Android中我有“ allow_dev ”,如 int
这是登录方式:
private void login(String email, String pass) {
showProgress();
JsonObject params = new JsonObject();
params.addProperty("username", email);
params.addProperty("password", pass);
UserOperationsTask task = new UserOperationsTask(UserOperationsTask.TaskMode.MODE_LOGIN, params) {
@Override
public void onLoadFinished(Bundle res) {
hideProgress();
String errorMessage = res.getString(UserOperationsTask.RESULT_ERROR_STRING);
if (errorMessage != null) {
showMessage(getString(R.string.login_error), getString(R.string.server_request_error));
} else {
String json = res.getString(UserOperationsTask.RESULT_JSON_STRING);
if (json != null) {
JsonParser parser = new JsonParser();
JsonObject responseData = parser.parse(json).getAsJsonObject();
JsonObject companyObj = responseData.getAsJsonObject("profile").getAsJsonObject("company");
}
setRegisteredMode();
}
}
};
task.execute(this);
}
此方法解析响应,我尝试将allow_dev类型从boolean转换为int,但我不明白我是否做得对吗?
private Bundle parseProfileResponse(Context context, JsonObject responseData) {
Log.d(TAG, "parseProfileResponse");
// I tried convert allow_dev type from boolean to int
String allow_dev_server = String.valueOf(responseData.get("allow_dev"));
boolean b = allow_dev_server.equals("true");
int allow_dev = b ? 1 : 0; // true == 1
Profile profile;
profile = GsonHolder.getGSONInstance().fromJson(responseData.getAsJsonObject("profile"), Profile.class);
profile.allow_dev = allow_dev;
Bundle res = new Bundle();
res.putParcelable(RESULT_OBJ, profile);
res.putString(RESULT_JSON_STRING, responseData.toString());
try {
Cache.saveToCache(context, profile);
} catch (RemoteException e) {
Log.d(TAG, "parseAuthResponse RemoteException: " + e.toString());
res.putString(RESULT_ERROR_STRING, context.getString(R.string.database_error));
} catch (OperationApplicationException e) {
Log.d(TAG, "parseAuthResponse OperationApplicationException: " + e.toString());
res.putString(RESULT_ERROR_STRING, context.getString(R.string.database_error));
}
return res;
}
我必须“ allow_dev ”将其转换为 int 并写入数据库。
答案 0 :(得分:0)
如果可以切换到映射,则可以使用静态类型可以提供的所有内容,与弱“类型”JsonElement
及其子类进行比较。它有几个优点:编译时检查,更强大的代码,IDE支持等。主要的缺点是你必须编写自定义映射,但是你有工具(在线也可以)尝试生成基于给定样本JSON的简单映射类(例如,这里非常流行的工具:http://www.jsonschema2pojo.org/)。
现在,让我们创建一些映射。像下面的映射很少使用:final
字段(用于“服务器响应”,不应该以编程方式修改; Gson无论如何都可以分配这些字段);对于非基元的null
和基本类型的一些hacks默认值来欺骗编译器(如Integer.value(0)
但不仅仅是0
:否则,javac
可以内联常量,因此Gson不能影响他们);没有getter / setter(数据传输对象只是数据包,但是,getter可以更好地工作)。无论如何,您可以使用您的样式,并且以下映射用于演示目的(映射代码甚至具有紧凑格式:每行折叠注释一个属性)。
final class Response<T> {
final Status status = null;
final T response = null;
}
final class Status {
final int error = Integer.valueOf(0);
final int code = Integer.valueOf(0);
final String message = null;
}
final class ProfileAndAccessToken {
final Profile profile = null;
@SerializedName("access_token") final String accessToken = null;
}
final class Profile {
final int id = Integer.valueOf(0);
final String username = null;
@SerializedName("full_name") final String fullName = null;
final String phone = null;
final int verified = Integer.valueOf(0);
final int admin = Integer.valueOf(0);
@SerializedName("allow_dev") @JsonAdapter(BooleanToIntTypeAdapter.class) final int allowDev = Integer.valueOf(0);
final Company company = null;
}
final class Company {
final int id = Integer.valueOf(0);
final String name = null;
final String email = null;
final String sample = null;
final URL logo = null;
}
注意上面的两个注释:
@SerializedName
- 此注释可以“重命名”字段,因此您甚至可以使用特殊字符(但不鼓励它,并且通常用于将传入的JSON属性名称映射到javaCamelNamingConventions)。@JsonAdapter
- 此注释可以将特殊类型的适配器“附加”到某个字段,以便它可以将JSON属性转换为给定字段,反之亦然。现在让我们实现一个类型适配器,它可以将传入的boolean
值转换为本地int
值,反之亦然。请注意,类型适配器以流方式工作,因此您必须在读取期间快速读取JSON令牌流,当然,在写入期间生成JSON令牌流。
final class BooleanToIntTypeAdapter
extends TypeAdapter<Integer> {
// Public constructors may be evil, and let expose as less as possible
// Gson can still instantiate this type adapter itself
private BooleanToIntTypeAdapter() {
}
@Override
@SuppressWarnings("resource")
public void write(final JsonWriter out, final Integer value)
throws IOException {
// If the given value is null, we must write the `null` token to the output JSON tokens stream anyway in order not to break JSON documents
if ( value == null ) {
out.nullValue();
return;
}
// Let's assume that we can accept either 0 or 1 that are mapped to false and true respectively
switch ( value ) {
case 0:
out.value(false);
break;
case 1:
out.value(true);
break;
default:
// Or throw an exception as fast as we can
throw new IllegalArgumentException("Cannot convert " + value + " to a boolean literal");
}
}
@Override
public Integer read(final JsonReader in)
throws IOException {
// Peek the next token type, and if it's null, then return null value too
if ( in.peek() == NULL ) {
return null;
}
// Otherwise parse the next token as boolean and map it either to 1 or 0
return in.nextBoolean() ? 1 : 0;
}
}
这就是你在Gson所需要的一切。现在,对于整个JSON,由于响应映射类是通用的,您必须告诉Gson T
是什么。 Gson在java.lang.reflect.Type
方法中接受fromJson
,这种类型可以包含原始类型和参数化类型,因此Gson可以(de)更准确地序列化。
private static final Type profileAndAccessTokenResponse = new TypeToken<Response<ProfileAndAccessToken>>() {
}.getType();
final Response<ProfileAndAccessToken> response = gson.fromJson(JSON, profileAndAccessTokenResponse);
System.out.println(response.response.profile.allowDev);
System.out.println(gson.toJson(response, profileAndAccessTokenResponse));
输出:
0
{ “状态”:{ “错误”:0 “代码”:200, “消息”: “OK”}, “响应”:{ “轮廓”:{ “ID”:114, “用户名”:“K @ gmail.com”, “FULL_NAME”: “K”, “手机”: “9999999”, “验证”:1, “管理员”:0, “allow_dev”:假的, “公司”:{ “ID”:9, “name”:“ООО\”Фингерзмедиа\“”,“email”:“info@fingers.by”,“logo”:“http://storage.guardian-glass.fingersmedia.by/0cb56968b3cec1bba301db8d51d1015e.jpg”}},“access_token”:“15629e234e04a54a5a44ef2aa4eccb1d”}} < / p>
请注意,第一行是0
:这是BooleanToIntTypeAdapter
生成的内容。回到你的代码:
String allow_dev_server = String.valueOf(responseData.get("allow_dev"));
boolean b = allow_dev_server.equals("true");
int allow_dev = b ? 1 : 0; // true == 1
Profile profile;
profile = GsonHolder.getGSONInstance().fromJson(responseData.getAsJsonObject("profile"), Profile.class);
profile.allow_dev = allow_dev;
可以用简单的替换:
final Profile profile = GsonHolder.getGSONInstance().fromJson(responseData.getAsJsonObject("profile"), Profile.class)
// now `Profile.allowDev` is 0 or 1 automatically
请注意,responseData
可以替换为特定的映射,因此您甚至无法在该行进行解析:可能您只需将整个response
对象作为类映射传递而不是{{1在你的JsonObject
中 - 它会更强大。