0

I'm trying to parsing some data from a Json File but I'm getting some problems.

This is my sample Data from the JSON file Data Source

And this is my code:

foreach (JsonValue groupValue in jsonArray)
{
    JsonObject groupObject = groupValue.GetObject();
    DadosLocaisInteresse group = 
        new DadosLocaisInteresse(
            groupObject["UniqueId"].GetString(),
            groupObject["Title"].GetString(),
            groupObject["Subtitle"].GetString(),
            groupObject["ImagePath"].GetString(),
            groupObject["Description"].GetString(),
            groupObject["Latitude"].GetNumber(),
            groupObject["Longitude"].GetNumber()
    );

    foreach (JsonValue itemValue in groupObject["Items"].GetArray())
    {
        JsonObject itemObject = itemValue.GetObject();

        group.Items.Add(
            new DadosLocaisInteressePontos(
                itemObject["UniqueId"].GetString(),
                itemObject["Title"].GetString(),
                itemObject["Subtitle"].GetString(),
                itemObject["ImagePath"].GetString(),
                itemObject["Description"].GetString(),
                itemObject["Content"].GetString(),
                itemObject["ItemLatitude"].GetNumber(),
                itemObject["ItemLongitude"].GetNumber())
        );

        foreach (JsonValue galeriaValue in itemObject["Galerias"].GetArray())
        {
            JsonObject galeriaObject = galeriaValue.GetObject();
            ItemsGaleria galeria = 
                new ItemsGaleria(
                    galeriaObject["UniqueID"].GetString(),
                    galeriaObject["ImagePath"].GetString(),
                    galeriaObject["ImagePath1"].GetString()
            );                           

        }
    }
    this.Groups.Add(group);
}

The Groups and Items Arrays works just fine. The problem is that I can't get the data from the "Galeria Array"... What I'm doing wrong???

2
  • 2
    It's Galeria in JSON and Galerias in your code. Commented May 16, 2014 at 13:44
  • @JossefHarush my exception is: An exception of type 'System.Collections.Generic.KeyNotFoundException' occurred in mscorlib.dll but was not handled in user code Additional information: The given key was not present in the dictionary. Commented May 16, 2014 at 13:47

1 Answer 1

1

You can use the below mentioned method to DeserializeObject

You have to create a class which can contain the properties like :

 public class Grp
{

    public string UniqueId { get; set; }
    public string Title { get; set; }
    public string Subtitle { get; set; }
    private List<item> _Items=new List<item>();

    public List<item> Items
    {
        get { return _Items; }
        set { _Items = value; }
    }


}
 public class item
{
    public string UniqueId { get; set; }
    public string Title { get; set; }
    public string Subtitle { get; set; }
}

and then you can add the using Newtonsoft.Json; to DeserializeObject;

var obj = JsonConvert.DeserializeObject<Grp>(s);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.