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 May 08, 2016 at 06:27 AM by Ashky for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by Ashky · May 07, 2016 at 09:49 PM · c#jsondictionaryinventory system

Getting KeyNotFoundException and I can't understand why

Hello Unity Community!

I'm trying to make an inventory system for my game, so I've read a bit on the subject and now I'm following a tutorial of using LitJson in Unity. However, when running my code I get "KeyNotFoundException: The given key was not present in the dictionary.".

Here is my code:

 using UnityEngine;
 using System.Collections;
 using LitJson;
 using System.Collections.Generic;
 using System.IO;
 
 public class script_ItemDatabase : MonoBehaviour {
     private List<Item> database = new List<Item>();
     JsonData itemData;
 
     void Start()
     {
         itemData = JsonMapper.ToObject(File.ReadAllText(Application.dataPath+"/StreamingAssets/Items.json"));
         ConstructItemDatabase();
     }
 
     public Item GetItemByID(int index)
     {
         for(int i = 0; i < database.Count; i++)
         {
             if(database[i].ID==index)
             return database[i];
         }
         return null;
         
     }
 
     void ConstructItemDatabase()
     {
         for(int i = 0; i < itemData.Count; i++)
         {
             database.Add(new Item((int)itemData[i]["id"],
                                    itemData[i]["name"].ToString(),
                                    itemData[i]["description"].ToString(),
                                    (int)itemData[i]["damage"],
                                    (int)itemData[i]["firerate"],
                                    (bool)itemData[i]["stackable"],
                                    itemData[i]["slug"].ToString()));
         }
     }
 
 }

And here's my json:

 {
     {
         "id":0,
         "name":"Test Gun",
         "description":"It's a gun.",
         "damage":3,
         "firerate":2,
         "stackable":false,
         "slug":"duck_yellow"
     }
 }

The error shows up on the itemData = JsonMapper.ToObject(File.ReadAllText(Application.dataPath+"/StreamingAssets/Items.json")); line. Since I'm new to this subject I would like to understand why the error shows up, what does it actually mean, and, of course, how can I fix it? Am I using the library incorrectly? Did I set up my json improperly?

I know there's a lot of questions and answers on the web regarding this error, but so far I don't have enough experience using dictionaries to fix my code on my own.

Thanks for your time!

-Ashky

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

  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · May 07, 2016 at 11:42 PM

This is not a valid JSON notation. The curly brackets represents an object. Objects always contain key-value pairs. It looks like you want an array with several objects in there, so your JSON representation should look like this;

  [
      {
          "id":0,
          "name":"Test Gun",
          "description":"It's a gun.",
          "damage":3,
          "firerate":2,
          "stackable":false,
          "slug":"duck_yellow"
      }
  ]

Note the square brackets which represents an array.

Comment
Add comment · Show 2 · 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 Ashky · May 08, 2016 at 06:27 AM 0
Share

Wow such a rookie mistake on my behalf. Thanks a lot!

avatar image Bunny83 Ashky · May 08, 2016 at 11:36 AM 0
Share

No problem ^^. Actually i recently work a lot with LUA and this could have happend to me as well. In LUA there is only one complex type: "table". It can represent both, an array or an object and has "almost" the same syntax as JSON.

 -- LUA
 {
    {
       id = 0,
       name = "Name1",
    },
    {
       id = 1,
       name = "Name2",
    }
 }
 
 // JSON
 [
    {
       "id" : 0,
       "name" : "Name1",
    },
    {
       "id" : 1,
       "name" : "Name2",
    }
 ]

As you can see, quite similar. JSON uses a colon for the key-value pairs, a LUA table uses the equals sign.

Follow this Question

Answers Answers and Comments

141 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

Related Questions

Unity Inventory Database ID Error 1 Answer

SHA512 JS -> C# 0 Answers

Insert string into empty list at a specific index 0 Answers

Deserialize Json array with mixed types 0 Answers

Json to array of objects C# 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