- 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
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
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!
Oh my god... You saved my life... I spent 2 hours looking for this answer D: