Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Gillissie · Mar 14, 2011 at 04:58 PM · jsonlitjson

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Gillissie · Mar 15, 2011 at 12:07 AM 0
Share

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Gillissie · Mar 15, 2011 at 04:25 AM

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");
             }
         }
     }
 }

}

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mike 3 · Mar 15, 2011 at 04:56 AM 0
Share

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

avatar image Gillissie · Mar 15, 2011 at 06:53 AM 0
Share

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

avatar image Gillissie · Mar 15, 2011 at 07:02 AM 0
Share

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

avatar image Gillissie · Mar 15, 2011 at 07:10 AM 0
Share

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/

avatar image Mike 3 · Mar 15, 2011 at 10:51 AM 0
Share

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

Show more comments
avatar image
1

Answer by francksitbon · Dec 28, 2011 at 10:21 AM

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?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sudhaMR · Jun 19, 2015 at 11:36 AM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to read an int array using LitJson? 1 Answer

LitJson not working correctly 2 Answers

How to get object array from json? 0 Answers

Litjson autoformat 1 Answer

LitJson load float 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges