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 /
This question was closed Dec 01, 2015 at 01:36 PM by laharl219 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by laharl219 · Nov 25, 2015 at 03:21 PM · c#script.jsonfileread

Help, Reading a Json file (getting error KeyNotFounfExeption)

hello, i'm using a c# script to read a jsonfile i created, having a public class ,constructor. btw using LitJson for json compatibility.... I wanted to read the file, and debug.log it.

here is my script

 using LitJson;
 using System.Collections.Generic;
 using System.IO;
 using UnityEngine;
 
 public class Base_campos : MonoBehaviour
 {
     private List<Campos> database = new List<Campos>();
     private JsonData CamposData;
 
     void Start()
     {
         // Campos campos = new Campos(0, "tste", "");//cria instancia/copia
         //database.Add(campos);
         //Debug.Log(database[0].Texto);
         CamposData = JsonMapper.ToObject(File.ReadAllText(Application.dataPath + "/StreamingAssets/campos.json.json"));
         ConstructCamposDatabase();
 
         Debug.Log(database[0].Texto);
     }
 
     void ConstructCamposDatabase()
     {
         for (int i = 0; i < CamposData.Count; i++)
         {
             database.Add(new Campos((int)CamposData[i]["id"], CamposData[i]["texto"].ToString(), CamposData[i]["link"].ToString()));
         }
     }
 }
 
 public class Campos
 {
     public int ID { get; set; }
     public string Texto { get; set; }
     public string Link { get; set; }
 
     public Campos(int id, string texto, string link)
     {
         this.ID = id;
         this.Texto = texto;
         this.Link = link;
     }
 }
 

i'm getting the error:

 KeyNotFoundException: The given key was not present in the dictionary.
 System.Collections.Generic.Dictionary`2[System.Int32,System.Int32[]].get_Item (Int32 key) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:150)
 LitJson.JsonReader.Read ()
 Rethrow as JsonException: Invalid token '123' in input string
 LitJson.JsonReader.Read ()
 LitJson.JsonMapper.ReadValue (LitJson.WrapperFactory factory, LitJson.JsonReader reader)
 LitJson.JsonMapper.ReadValue (LitJson.WrapperFactory factory, LitJson.JsonReader reader)
 LitJson.JsonMapper.ToWrapper (LitJson.WrapperFactory factory, System.String json)
 LitJson.JsonMapper.ToObject (System.String json)
 Base_campos.Start () (at Assets/scripts/Base_campos.cs:16)
 

Any idea on how to solve it?

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
2
Best Answer

Answer by laharl219 · Nov 25, 2015 at 08:57 PM

After some research i could get my answer.... What happens is that my Json file had a failure, there was a "," that wasn't suposed to be in that file, making it not work.....the script is workong tho!

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 raserra12 · Oct 20, 2016 at 11:06 PM 0
Share

Oh my god... You saved my life... I spent 2 hours looking for this answer D:

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Simple way to get the most recently updated file and output to screen? C# Unity 0 Answers

C# Convert json arrays to unity arrys 3 Answers

Multiple Cars not working 1 Answer

An Utterly Baffling Problem 1 Answer

C# Converting json arrays using JsonUtility, 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