• 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.filejsonread

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:

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Distribute terrain in zones 3 Answers

How to get object array from json? 0 Answers

How can I save data to a text/json file and read it back and then assign the values back to the variables ? 2 Answers

Write and read text files with c# 1 Answer

VBScript through C# in Unity 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges