“`plaintext 将List转换为JSON会得到不完整的JSON列表 “`

huangapple go评论77阅读模式
英文:

List<SkuDetails> to json gives incomplete json list

问题

我收到一个 List&lt;SkuDetails&gt;,如下所示:

public void onSkuDetailsResponse(BillingResult billingResult,
                                List&lt;SkuDetails&gt; skuDetailsList) {
    Gson gson = new Gson();
    String json = gson.toJson(skuDetailsList);
    Log.d(TAG, json);

SkuDetails 文档

然后我得到了这个未完成的 JSON:

[{&quot;zza&quot;:&quot;{\&quot;skuDetailsToken\&quot;:\&quot;s-r3R-r\&quot;,\&quot;productId\&quot;:\&quot;chill_lo_fi\&quot;,\&quot;type\&quot;:\&quot;inapp\&quot;,\&quot;price\&quot;:\&quot;R$&#160;5,49\&quot;,\&quot;price_amount_micros\&quot;:5490000,\&quot;price_currency_code\&quot;:\&quot;BRL\&quot;,\&quot;title\&quot;:\&quot;Chill Lo-Fi (SDoubleKick - Footdrum - SS)\&quot;,\&quot;description\&quot;:\&quot;Chill Lo-Fi 声音包\&quot;}&quot;,&quot;zzb&quot;:{&quot;nameValuePairs&quot;:{&quot;skuDetailsToken&quot;:&quot;s-r3R-r&quot;,&quot;productId&quot;:&quot;chill_lo_fi&quot;,&quot;type&quot;:&quot;inapp&quot;,&quot;price&quot;:&quot;R$&#160;5,49&quot;,&quot;price_amount_micros&quot;:5490000,&quot;price_currency_code&quot;:&quot;BRL&quot;,&quot;title&quot;:&quot;Chill Lo-Fi (SDoubleKick - Footdrum - SS)&quot;,&quot;description&quot;:&quot;Chill Lo-Fi 声音包&quot;}}},{&quot;zza&quot;:&quot;{\&quot;skuDetailsToken\&quot;:\&quot;a-r\&quot;,\&quot;productId\&quot;:\&quot;percussion_and_fx\&quot;,\&quot;type\&quot;:\&quot;inapp\&quot;,\&quot;price\&quot;:\&quot;R$&#160;5,49\&quot;,\&quot;price_amount_micros\&quot;:5490000,\&quot;price_currency_code\&quot;:\&quot;BRL\&quot;,\&quot;title\&quot;:\&quot;Percussion and Fx (SDoubleKick - Footdrum - SS)\&quot;,\&quot;description\&quot;:\&quot;Percussion 和 Fx 声音包\&quot;}&quot;,&quot;zzb&quot;:{&quot;nameValuePairs&quot;:{&quot;skuDetailsToken&quot;:&quot;s-dsf3

你可以看到这是一个列表,它应该已经正确打印出来了,但是我最后得到了:

&quot;zzb&quot;:{&quot;nameValuePairs&quot;:{&quot;skuDetailsToken&quot;:&quot;s-dsf3

因此,当我将这个 gson 重新构建成一个对象时,我会得到一个异常。

英文:

I receive a List&lt;SkuDetails&gt; as following:

public void onSkuDetailsResponse(BillingResult billingResult,
                                List&lt;SkuDetails&gt; skuDetailsList) {
    Gson gson = new Gson();
    String json = gson.toJson(skuDetailsList);
    Log.d(TAG, json);

SkuDetails doc

Then I get this unfinished json:

[{&quot;zza&quot;:&quot;{\&quot;skuDetailsToken\&quot;:\&quot;s-r3R-r\&quot;,\&quot;productId\&quot;:\&quot;chill_lo_fi\&quot;,\&quot;type\&quot;:\&quot;inapp\&quot;,\&quot;price\&quot;:\&quot;R$&#160;5,49\&quot;,\&quot;price_amount_micros\&quot;:5490000,\&quot;price_currency_code\&quot;:\&quot;BRL\&quot;,\&quot;title\&quot;:\&quot;Chill Lo-Fi (SDoubleKick - Footdrum - SS)\&quot;,\&quot;description\&quot;:\&quot;Chill Lo-Fi sound pack\&quot;}&quot;,&quot;zzb&quot;:{&quot;nameValuePairs&quot;:{&quot;skuDetailsToken&quot;:&quot;s-r3R-r&quot;,&quot;productId&quot;:&quot;chill_lo_fi&quot;,&quot;type&quot;:&quot;inapp&quot;,&quot;price&quot;:&quot;R$&#160;5,49&quot;,&quot;price_amount_micros&quot;:5490000,&quot;price_currency_code&quot;:&quot;BRL&quot;,&quot;title&quot;:&quot;Chill Lo-Fi (SDoubleKick - Footdrum - SS)&quot;,&quot;description&quot;:&quot;Chill Lo-Fi sound pack&quot;}}},{&quot;zza&quot;:&quot;{\&quot;skuDetailsToken\&quot;:\&quot;a-r\&quot;,\&quot;productId\&quot;:\&quot;percussion_and_fx\&quot;,\&quot;type\&quot;:\&quot;inapp\&quot;,\&quot;price\&quot;:\&quot;R$&#160;5,49\&quot;,\&quot;price_amount_micros\&quot;:5490000,\&quot;price_currency_code\&quot;:\&quot;BRL\&quot;,\&quot;title\&quot;:\&quot;Percussion and Fx (SDoubleKick - Footdrum - SS)\&quot;,\&quot;description\&quot;:\&quot;Percussion and Fx sound pack\&quot;}&quot;,&quot;zzb&quot;:{&quot;nameValuePairs&quot;:{&quot;skuDetailsToken&quot;:&quot;s-dsf3

You can see that this is a list, it should have printed correctly, but I get, in the end:

&quot;zzb&quot;:{&quot;nameValuePairs&quot;:{&quot;skuDetailsToken&quot;:&quot;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 &lt;= jsonText.length() / maxLogSize; i++) {
      int start = i * maxLogSize;
      int end = (i + 1) * maxLogSize;
      end = end &gt; 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);

huangapple
  • 本文由 发表于 2020年10月22日 09:57:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/64474123.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定