Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by andrey-koms-ua · Mar 12, 2017 at 03:19 PM · c#unity 5arraysjsonlitjson

How to read an int array using LitJson?

So i'm trying to generate level geometry simply by using Line Renderer and Edge Collider 2D. The level data is stored inside json file and looks like this:

 [
     {
         "id": 0,
         "title": "Some level",
         "spawnPoint": [0, 5],
         "finishPoint": 30,
         "xPoses": [0, 10, 20, 30],        
         "yPoses": [0, 5, 10, 15]        
     }
 ]

There is nothing wrong with reading single variables until it comes to arrays. I can't figure out how is it done in LitJson. In my project I have the Level class which looks like this:

 public class Level {
 
     public int id { get; set; }
     public string title { get; set; }
     public int[] spawnPoint { get; set; }
     public int[] finishPoint { get; set; }
     public float[] xPoses { get; set; }
     public float[] yPoses { get; set; }
 
 }

Levels are stored inside the List object and I load them like this

 levelData = JsonMapper.ToObject(File.ReadAllText(Application.dataPath + "/StreamingAssets/Levels/levels.json"));

 private List<Level> levelDatabase = new List<Level>();

So the main question is how to read a JSON array (spawnpoint, x/y Poses) and make it into the C# one. Thank you.

Comment
Add comment
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

1 Reply

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

Answer by andrey-koms-ua · Dec 16, 2017 at 10:47 PM

For those who stuck with a same problem, I found this workaround:

 using System;
 
 public static class JsonHelper {
 
     public static T[] FromJson<T>(string json) {
         Wrapper<T> wrapper = UnityEngine.JsonUtility.FromJson<Wrapper<T>>(json);
         return wrapper.Items;
     }



     public static string ToJson<T>(T[] array) {
         Wrapper<T> wrapper = new Wrapper<T>();
         wrapper.Items = array;
         return UnityEngine.JsonUtility.ToJson(wrapper, true);
     }
 
     [SerializableAttribute]
     private class Wrapper<T> {
         public T[] Items;
     }
 }


And then it's loaded like this:

 List<Level> levels = JsonHelper.FromJson<Level>(jsonString).ToList();
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 LicketyCut · Mar 26, 2021 at 09:37 PM 0
Share

Thanks andrey-koms-ua

This is very useful.

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can i get all of my users data on FireBase using Unity and RestClient json 1 Answer

Load json into array class 1 Answer

How to get a JSON string of a folder structure with c# in Unity3d 2 Answers

Help me with this json code please 1 Answer

How do I print each element of a string array separately to a UI text box 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