How to implement LitJSON?

I feel like a noob, but how do you implement LitJson into a Unity Project? I downloaded what appears to be the latest version from here http://sourceforge.net/projects/litjson/files/litjson/0.5.0/

However, I have no idea what to do with all the files to get it working in Unity. Thanks.

I'm not sure why someone voted down for this question.

2 Answers

2

Put all of the src/LitJson/*.cs files into a project folder named "Plugins". You can optionally create a subfolder in Plugins to keep the files organized from other scripts in there.

The JsonReader and JsonWriter functions work, but the JsonMapper functions don't work. There are other posts with people having problems with that, but with no answers.

The JsonReader allows you to get the raw data from the Json string, but I had to write my own function to make anything useful out of the raw data.

Here are my functions that you're free to use if you want. Just put the below code into a separate Javascript file. It simplifies getting data one property or array at a time, but doesn't support arrays of objects. These return only string values (or an array of strings), never objects. You have to know the data structure and request exactly what you're looking for. It's not going to be as fast as building objects with properties, but it's real easy to use and should be fast enough for small amounts of data.

function GetArray(json:String,property:String):String[]
{
    var val:String = GetValue(json,property);

    return val.Split(","[0]);
}

function GetValue(json:String,property:String):String
{
    // The property parameter is in dotted hierarchy format if digging deeper into object properties.
    // Examples: memberCount
    //           member1.name
    //           member1.instrument.type

    var reader:LitJson.JsonReader = new LitJson.JsonReader(json);

    var currentProperty:int = 0;

    var propertyArray:String[] = property.Split("."[0]);

    if (reader.Read())  // Call Read() one time to get past the first ObjectStart that is always required.
    {
        while (reader.Read())
        {
            var val:String = "";
            var token:String = reader.Token.ToString();

            if (currentProperty == propertyArray.Length - 1 && token == "ObjectEnd")
            {
                // We're as deep as the requested property, but we're at the end of the current object and haven't found the sought token, so just return "".
                return "";
            }

            if (reader.Value != null)
            {
                val = reader.Value.ToString();
            }

            if (token == "PropertyName" && val == propertyArray[currentProperty])
            {
                // The value is actually the next value after the PropertyName, so move ahead one.
                reader.Read();

                token = reader.Token.ToString();
                val = "";
                if (reader.Value != null)
                {
                    val = reader.Value.ToString();
                }

                if (currentProperty == propertyArray.Length - 1)
                {
                    // Can't go any deeper, so we found our final Token.
                    if (token == "ObjectStart")
                    {
                        // If an object is the requested property, then return "" since we can't return an object. This shouldn't happen.
                        return "";
                    }
                    else
                    {
                        if (token == "ArrayStart")
                        {
                            // Return the contents of the array as a comma-delimited string.
                            val = "";

                            while (reader.Read())
                            {                               
                                if (reader.Token.ToString() == "ArrayEnd")
                                {
                                    break;
                                }

                                if (val != "")
                                {
                                    val += ",";
                                }

                                if (reader.Value == null)
                                {
                                    val += "";
                                }
                                else
                                {
                                    val += reader.Value.ToString();
                                }
                            }
                        }
                        return val;
                    }
                }

                if (token == "ObjectStart")
                {
                    // Dig deeper into the next object if it matches our Token.
                    currentProperty++;
                }
            }
            else
            {
                // If at an object start, skip past the object and get to the next token.
                if (token == "ObjectStart")
                {
                    do
                    {
                        reader.Read();
                    } while (reader.Token.ToString() != "ObjectEnd");
                }
            }
        }
    }
}

You only need the dll file, no need to put the whole source folder into unity (which will make compiling much slower). If you do that, don't put it in Plugins, anywhere but there is fine for it

I don't see a dll file in the download.

By the way, I'm using Unity on Mac. Will dll's work on Mac?

I found the dll. It is in a different zip file with the dll included. Not sure why they'd have a separate zip file with it and without it. http://sourceforge.net/projects/litjson/files/litjson/0.5.0/

Yeah, should (well does, tried it earlier) work fine on a mac, managed code not native code :)

Hi, I’m trying to understand your code, but I don’t find a way to launch it:

I use this json : http://paultondeur.com/files/2010/UnityExternalJSONXML/books.json

and trying to access the second book

// The property parameter is in dotted
hierarchy format if digging deeper
into object properties.
// Examples: memberCount
// member1.name
// member1.instrument.type

I’m trying with :

book.id.title
book.id.2.title
book.2.title

2.title

But each time I got a null return from the function

Which property parameter should I call?

So, did you figure it out? I am, using the same json and couldn't load it! Though I didn't find any compilation error.