• 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 /
avatar image
0
Question by xGrospitch · Mar 10, 2017 at 06:47 AM · jsontypecasting

Instance of JsonData Doesn't hold an int even though im typcasting..

Hey guys so i'm writing an inventory script passing data using JSON. I started following a guide online however I am casting the same way as him yet I am getting an error.

Here is my script I'm using (on a inventory game object in scene) using System.Collections; using System.Collections.Generic; using UnityEngine; using LitJson; using System.IO;

 public class ItemDatabase : MonoBehaviour {
 
     //creating a list to hold our database called databse
     private List<Item> database = new List<Item>();
     //creates jsondata object that holds all the data we pull in from the string
     private JsonData itemData;
 
 
     void Start()
     {
         //changes json data to a object this is pulling from streaming assets folder in assets from game (Streaming assets does not get built on build) people cant change items
         //works as a dictionary
         itemData = JsonMapper.ToObject (File.ReadAllText(Application.dataPath + "/StreamingAssets/Items.json"));
 
         //running construct item method
         ConstructItemDatabase ();
 
         Debug.Log (database[1].Title);
     }
 
     //set up system to laod data from the json file
     void ConstructItemDatabase()
     {
         //loops through the item data list to add it to the ItemDatabase
         for (int i = 0; i < itemData.Count; i++)
         {
             database.Add (new Item ((int)itemData[i]["id"], itemData[i]["title"].ToString(), (int)itemData[i]["value"]));
         }
     }
 }
     
 
 //this will hold all the properites of the item ex. stats id name
 public class Item
 {
     public int ID { get; set;}
     public string Title { get; set;}
     public int Value { get; set; }
 
     public Item(int id, string title, int value)
     {
         this.ID = id;
         this.Title = title;
         this.Value = value;
     }
 }

This is my JSON script

 [
     {
         "id": 0,
         "title": "Shitty Sword",
         "value": "6"
     },
     {
         "id": 1,
         "title": "Steel Gloves",
         "value": "123"
     }
 ]


I know it was to do with the way I am typecasting in the ConstructItemDatabase() but I dont understand why..

Please help Thanks

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

Answer by C-Hoy · Mar 13, 2017 at 02:59 AM

@xGrospitch

in your JSON script you're declaring the int in "value": between two " "

"value": "6" should actually be :

"value": 6

i made the same mistake and just realized it when i was googling to find the answer xD

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 clayton1314 · Jul 31, 2017 at 03:07 PM 0
Share

thanks man u saved me

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

97 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

Related Questions

Deserialize Json into Object 0 Answers

Serialization error on loading a scene 0 Answers

Unity imposed recursion depth of Json data? 0 Answers

Read and Write Textfile on mobile (C# Unity ) 0 Answers

gameObject from jso 0 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