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 /
  • Help Room /
This question was closed Jul 25, 2018 at 03:48 PM by UnityCoach for the following reason:

Lead to sufficient material was provided.

avatar image
1
Question by this_Jamal · Jul 25, 2018 at 11:39 AM · unity 5jsonserializable

Reading json file

how do I read?

Json:

       {
          "data":[[1.0, 1.5,2.0],[4.0, 4.5, 5.0]]
       }

C#:

       public class test : MonoBehaviour {
       
        string path;
        string jsonString;

        void Start () {
         path = Application.streamingAssetsPath + "/acadunity.json";
         jsonString = File.ReadAllText(path);
         Creature Lines = JsonUtility.FromJson<Creature>(jsonString);
         print(Lines.data[0]);
        }
       }
       [Serializable]
       public class Creature{
        public int[] data;
       }

ERROR!!!!

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 UnityCoach · Jul 25, 2018 at 12:00 PM 1
Share

I'm not sure this is the only problem, but your class data field is of type int [], and the data in the json file contains Vectors.

1 Reply

  • Sort: 
avatar image
1

Answer by UnityCoach · Jul 25, 2018 at 12:03 PM

Try this :

 [Serializable]
 public class Creature
 {
     public Vector3 [] data;
 }
Comment
Add comment · Show 9 · 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 this_Jamal · Jul 25, 2018 at 01:37 PM 0
Share

I get:

 (0.0, 0.0, 0.0)

json:

 {
 "lines":[[1,0.3544,0.0],[5.0,0.0,0.0]
 ]}

avatar image UnityCoach this_Jamal · Jul 25, 2018 at 02:01 PM 0
Share

Did you try the opposite ? Serializing an object and look at the son file ?

avatar image this_Jamal UnityCoach · Jul 25, 2018 at 02:41 PM 0
Share

No, is there another way to read? and I want to read the first number "1" can you tell me how?

Show more comments
avatar image this_Jamal · Jul 28, 2018 at 08:18 AM 0
Share

json:

 {
    "number":[
                {"x":439.0,"y":234.0,"z":342.0},{"x":1.0,"y":2.0,"z":3.0}
             ]
 }

any idea? to get x or y or z.

avatar image UnityCoach this_Jamal · Jul 28, 2018 at 10:26 AM 0
Share
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System;
 using System.IO;
 
 public class JsonTest : $$anonymous$$onoBehaviour
 {
     [Serializable]
     public class TestClass
     {
         public Vector3 testValue = Vector3.right;
         public Vector3 [] testValues = new Vector3[2] {Vector3.back, Vector3.up};
     }
 
     [SerializeField] TestClass testClass;
 
     string GetPath ()
     {
         string path = Path.Combine(Application.dataPath, "test.json");
         Debug.Log(path);
         return path;
     }
 
     [Context$$anonymous$$enu ("Save")]
     void Save ()
     {
         string jsonString = JsonUtility.ToJson(testClass, true);
         StreamWriter writer = new StreamWriter (GetPath());
         writer.Write(jsonString);
         writer.Close();
     }
 
     [Context$$anonymous$$enu ("Load")]
     void Load ()
     {
         StreamReader reader = new StreamReader (GetPath());
         string jsonString = reader.ReadToEnd();
         reader.Close();
         JsonUtility.FromJsonOverwrite(jsonString, testClass);
     }
 }
 
 /* Json Output
 {
     "testValue": {
         "x": 1.0,
         "y": 0.0,
         "z": 0.0
     },
     "testValues": [
         {
             "x": 0.0,
             "y": 0.0,
             "z": -1.0
         },
         {
             "x": 0.0,
             "y": 1.0,
             "z": 0.0
         }
     ]
 }
 */

Follow this Question

Answers Answers and Comments

241 People are following this question.

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

Related Questions

Deserialize JSON with a certain structure using JsonUtility 1 Answer

Json Utilities returns empty object (javascript) 2 Answers

JsonUtility returns {} 2 Answers

How to reduce the number of WWW requests? 0 Answers

can you read json file on android ? I have a thesis and it's not going well. 1 Answer


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