Android 4.1.2上奇怪的JSON编码问题
我在安卓4.1.2上遇到了一个问题,RESTAPI提供给我们的JSON对象在返回时会被奇怪地编码 这是我得到的json片段:Android 4.1.2上奇怪的JSON编码问题,android,json,encoding,utf-8,Android,Json,Encoding,Utf 8,我在安卓4.1.2上遇到了一个问题,RESTAPI提供给我们的JSON对象在返回时会被奇怪地编码 这是我得到的json片段: "cost":{ "amount": 0, "currency": "GBP" } 我想以同样的方式(修改json的其他部分)传回这个特定的代码片段,但这是我在Android 4.1.2上得到的: "cost":"{amount=0, currency=GBP}" 我认为导致这种奇怪编码的函数如下: private StringEnti
"cost":{
"amount": 0,
"currency": "GBP"
}
我想以同样的方式(修改json的其他部分)传回这个特定的代码片段,但这是我在Android 4.1.2上得到的:
"cost":"{amount=0, currency=GBP}"
我认为导致这种奇怪编码的函数如下:
private StringEntity getEntityForRequest(final Payment payment, final PaymentDelegate delegate) {
JSONObject json = new JSONObject();
MyApplication.getContext().addApplicationInformationToJSONObject(json);
StringEntity entity = null;
try {
entity = new StringEntity(json.toString(), "UTF-8");
} catch (UnsupportedEncodingException e1) {
payment.markAsFailed("Reservation failed, data returned not expected.");
save(payment);
if (delegate != null) {
delegate.onFailure(new MyError(MyError.DEFAULT_STATUS, MyError.DEFAULT_TYPE, "Payment error", "Error during reservation"));
}
}
return entity;
}
这是AddApplicationFormationToJSONObject函数:
/**
* Adds system information to a JSON object.
*/
public void addApplicationInformationToJSONObject(JSONObject json) {
try {
try {
json.put("app_version", getPackageManager().getPackageInfo(getPackageName(), 0).versionName);
} catch (NameNotFoundException e) {
json.put("app_version", "Unknown");
}
json.put("device", getDeviceName());
json.put("os_type", "android");
json.put("os_version", String.format("%d", Build.VERSION.SDK_INT));
json.put("device_id", Secure.getString(getContext().getContentResolver(), Secure.ANDROID_ID));
} catch (JSONException e) {
MyLog.e("MyApplication", "Error when adding system information to JSON");
}
}
是什么导致这种奇怪的编码
如何修改代码以避免此类问题?找到了解决方案。旧版本似乎将成本片段解释为字符串而不是JSONObject。这样做似乎可以解决问题:
ticketObject.remove("cost");
ticketObject.put("cost", new JSONObject(getCost()));
我最初以为这只是在三星Galaxy S2上,但我已经验证(使用模拟器)这可以在任何Android 4.1.2上复制。需要更清楚地回答您的问题您的
向JSONObject(json)添加了什么应用程序信息
method do?@agamov我现在已经包含了该函数。@IndraKumarS您需要更清楚地说明什么?我尽力了。