Search Unity

Deserialize JSON.

Discussion in 'Scripting' started by Master-Frog, Jun 30, 2015.

  1. Master-Frog

    Master-Frog

    Joined:
    Jun 22, 2015
    Posts:
    2,302
    I'll cut to the chase, I'm pulling JSON down over an HTTP request... it's already in a string.

    All I want to do is parse that JSON into some kind of collection. Array, List, doesn't matter for my purposes.

    The JSON formatting is my choice coming off of my server, I can use a different format but I want to try to stick with JSON. However, there doesn't seem to be any library for this...........?

    In PHP I can take an array and just make it into JSON. It's one line.

    So, what gives? Is there any rational way to do this?
     
  2. Iron-Warrior

    Iron-Warrior

    Joined:
    Nov 3, 2009
    Posts:
    838
    JSON.Net is the way to go, you can install it through NuGet.
     
  3. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    The official Newtonsoft JSON package will not work on iOS, WebGL, Android, WebPlayer, Windows Store (from within Unity), etc. If you need JSON .NET you'd need to use my port however it sounds like you may have a very simple scenario so it might be overkill and you'd get by with something like JsonFX, miniJSON or JsonObject.
     
    Iron-Warrior and Master-Frog like this.
  4. Master-Frog

    Master-Frog

    Joined:
    Jun 22, 2015
    Posts:
    2,302
    Yeah, I'm just transmitting two related fields in pairs. What's funny is it got me thinking... unless there are nested objects involved, JSON and XML are overkill, because you just need a delimiter to split the objects and a delimiter to split the fields.
     
    Dustin-Horne likes this.
  5. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Very true. In fact you could use a Tuple and do it generically. Or just separate them with a bar character or something.
     
    Master-Frog likes this.
  6. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,657
    We have built-in JSON serialisation and deserialisation support on the way, but not until December at the earliest.
     
  7. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    I saw that. It'll be interesting how robust it was. It looks like it's associated with the web request support. I'm guessing it probably won't handle any complex scenarios like polymorphism and selective serialization / deserialization via converters and attributes.

    One thing I can recommend to you guys is to be very mindful with your stripping. I'm getting tons of bug reports since Unity 5.1 in Android and iOS (still working on putting some repro projects together), but the majority of them seem to be because setters on properties are getting stripped so the properties still exist but just the setters are gone and aren't located via reflection.
     
  8. Master-Frog

    Master-Frog

    Joined:
    Jun 22, 2015
    Posts:
    2,302
    ...right there, that's why I try to do things as simple as possible. Never know when something will just "not work".