Search Unity

How to pretty print with JsonUtil to existing json?

Discussion in 'Scripting' started by MrLucid72, Sep 22, 2016.

  1. MrLucid72

    MrLucid72

    Joined:
    Jan 12, 2016
    Posts:
    995
    I noticed that if I took existing json and used `JsonUtility.FromJson(json, true);` it will print nothing.

    Does this mean I have to convert to a random type, then BACK to json again to print pretty? Surely there's a better way, if anyone can enlighten me :)
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    JsonUtility.FromJson(json, true); won't compile - that method doesn't exist.

    JsonUtility.ToJson(json, true); will compile, but it's going to try to jsonify your json, which I assume would fail.


    PrettyPrinting isn't that hard to do on your own - add spaces between symbols, and add as many tabs as there's non-matched left-braces. Otherwise, yes, the only way to do this as of now is to do JsonUtility.ToJson(JsonUtility.FromJson(json), true);

    EDIT: or download some json library that contains a prettyfy-method.
     
    dginovker likes this.
  3. MrLucid72

    MrLucid72

    Joined:
    Jan 12, 2016
    Posts:
    995
    oops typo, I used ToJson and yea it failed ;)
     
  4. unity_N7Mv18UwkDRu_A

    unity_N7Mv18UwkDRu_A

    Joined:
    Sep 25, 2017
    Posts:
    2
  5. nalani5210

    nalani5210

    Joined:
    Mar 18, 2021
    Posts:
    1