英文:
List<SkuDetails> to json gives incomplete json list
问题
我收到一个 List<SkuDetails>
,如下所示:
public void onSkuDetailsResponse(BillingResult billingResult,
List<SkuDetails> skuDetailsList) {
Gson gson = new Gson();
String json = gson.toJson(skuDetailsList);
Log.d(TAG, json);
然后我得到了这个未完成的 JSON:
[{"zza":"{\"skuDetailsToken\":\"s-r3R-r\",\"productId\":\"chill_lo_fi\",\"type\":\"inapp\",\"price\":\"R$ 5,49\",\"price_amount_micros\":5490000,\"price_currency_code\":\"BRL\",\"title\":\"Chill Lo-Fi (SDoubleKick - Footdrum - SS)\",\"description\":\"Chill Lo-Fi 声音包\"}","zzb":{"nameValuePairs":{"skuDetailsToken":"s-r3R-r","productId":"chill_lo_fi","type":"inapp","price":"R$ 5,49","price_amount_micros":5490000,"price_currency_code":"BRL","title":"Chill Lo-Fi (SDoubleKick - Footdrum - SS)","description":"Chill Lo-Fi 声音包"}}},{"zza":"{\"skuDetailsToken\":\"a-r\",\"productId\":\"percussion_and_fx\",\"type\":\"inapp\",\"price\":\"R$ 5,49\",\"price_amount_micros\":5490000,\"price_currency_code\":\"BRL\",\"title\":\"Percussion and Fx (SDoubleKick - Footdrum - SS)\",\"description\":\"Percussion 和 Fx 声音包\"}","zzb":{"nameValuePairs":{"skuDetailsToken":"s-dsf3
你可以看到这是一个列表,它应该已经正确打印出来了,但是我最后得到了:
"zzb":{"nameValuePairs":{"skuDetailsToken":"s-dsf3
因此,当我将这个 gson 重新构建成一个对象时,我会得到一个异常。
英文:
I receive a List<SkuDetails>
as following:
public void onSkuDetailsResponse(BillingResult billingResult,
List<SkuDetails> skuDetailsList) {
Gson gson = new Gson();
String json = gson.toJson(skuDetailsList);
Log.d(TAG, json);
Then I get this unfinished json:
[{"zza":"{\"skuDetailsToken\":\"s-r3R-r\",\"productId\":\"chill_lo_fi\",\"type\":\"inapp\",\"price\":\"R$ 5,49\",\"price_amount_micros\":5490000,\"price_currency_code\":\"BRL\",\"title\":\"Chill Lo-Fi (SDoubleKick - Footdrum - SS)\",\"description\":\"Chill Lo-Fi sound pack\"}","zzb":{"nameValuePairs":{"skuDetailsToken":"s-r3R-r","productId":"chill_lo_fi","type":"inapp","price":"R$ 5,49","price_amount_micros":5490000,"price_currency_code":"BRL","title":"Chill Lo-Fi (SDoubleKick - Footdrum - SS)","description":"Chill Lo-Fi sound pack"}}},{"zza":"{\"skuDetailsToken\":\"a-r\",\"productId\":\"percussion_and_fx\",\"type\":\"inapp\",\"price\":\"R$ 5,49\",\"price_amount_micros\":5490000,\"price_currency_code\":\"BRL\",\"title\":\"Percussion and Fx (SDoubleKick - Footdrum - SS)\",\"description\":\"Percussion and Fx sound pack\"}","zzb":{"nameValuePairs":{"skuDetailsToken":"s-dsf3
You can see that this is a list, it should have printed correctly, but I get, in the end:
"zzb":{"nameValuePairs":{"skuDetailsToken":"s-dsf3
Because of that, when I reconstruct this gson into an object, I get an exception
答案1
得分: 1
Gson
不会截断您的JSON数据。问题出在logcat
,默认情况下,它会截断任何被视为过长的日志消息。这可能发生在您的IDE内部,也可能发生在通过命令行运行logcat
时。要消除日志截断,您可以将要记录的字符串拆分,并创建一个类似这样的包装器:
package com.github.fundamentals.array;
public class Log {
public static void d(String tag, String jsonText) {
int maxLogSize = 2000;
for (int i = 0; i <= jsonText.length() / maxLogSize; i++) {
int start = i * maxLogSize;
int end = (i + 1) * maxLogSize;
end = end > jsonText.length() ? jsonText.length() : end;
android.util.Log.d(tag, jsonText.substring(start, end));
}
}
}
您还可以在GsonBuilder
上使用setPrettyPrinting
,以人类可读的格式打印JSON字符串(用于调试):
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String prettyJson = gson.toJson(json);
Log.d(TAG, prettyJson);
英文:
Gson
will not truncate your JSON data. logcat
is the culprit here, by default, it will truncate any log message that it considers to be too long. This can happen both inside of your IDE and when running logcat
on the command line. To get rid of the log capping you can split the string you want to log with creating a wrapper something like this:
package com.github.fundamentals.array;
public class Log {
public static void d(String tag, String jsonText) {
int maxLogSize = 2000;
for (int i = 0; i <= jsonText.length() / maxLogSize; i++) {
int start = i * maxLogSize;
int end = (i + 1) * maxLogSize;
end = end > jsonText.length() ? jsonText.length() : end;
android.util.Log.d(tag, jsonText.substring(start, end));
}
}
}
You can also use setPrettyPrinting
on GsonBuilder
to print a JSON string in a human-readable format (for debugging)
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String prettyJson = gson.toJson(json);
Log.d(TAG, prettyJson);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论