簡體   English   中英

Json漂亮打印失敗

[英]Json pretty print fails

我的Android API 15級有此代碼

        JSONArray l = new JSONArray();
        JSONObject a = new JSONObject();
        a.put("a", "a");
        a.put("b", "a");
        a.put("c", "d");
        l.put(a);
        a = new JSONObject();
        a.put("d", "a");
        a.put("g", "3");
        l.put(a);

        Log.d(TAG, l.toString(3));
        Log.d(TAG, l.toString());

輸出是這個

DEBUG   test    [
DEBUG   test       {
DEBUG   test          "b": "a",
DEBUG   test          "c": "d",
DEBUG   test          "a": "a"
DEBUG   test       },
DEBUG   test          "g": "3",
DEBUG   test          "d": "a"
DEBUG   test       }
DEBUG   test    ]
DEBUG   test    [{"b":"a","c":"d","a":"a"},{"g":"3","d":"a"}]

漂亮的打印輸出缺少第二個JSONObject的開頭{。 這是一個已知的錯誤 ?

(使用Gson時也是如此。)

編輯:

它對我來說運作良好,我正在使用Intellij:

10-15 14:18:43.433: DEBUG/it.enrichman.myapp(4470): [
        {
        "b": "a",
        "c": "d",
        "a": "a"
        },
        {
        "g": "3",
        "d": "a"
        }
        ]

所以我想Eclipse(照常)搞亂了日志。 :)


為什么這應該是一個錯誤? 這是預期的行為。

toString(int indentSpaces)會漂亮地打印出來,普通的toString()只會輸出json。 它也寫在文檔中:

公共字符串toString()

將此數組編碼為緊湊的JSON字符串

public String toString(int indentSpaces)

將此數組編碼為可讀的JSON字符串以進行調試

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM