0

I'm having a hard time deserializing a JSON file I'm getting from an API. This is the class structure I have created based on the file:

namespace Huskeliste {
 public class RecipeClass {
  public class Rootobject {
    public string q { get; set; }
    public int from { get; set; }
    public int to { get; set; }
    public bool more { get; set; }
    public int count { get; set; }
    public Hit[] hits { get; set; }
  }

  public class Hit {
    public Recipe recipe { get; set; }
    public bool bookmarked { get; set; }
    public bool bought { get; set; }
  }

  public class Recipe {
    public string uri { get; set; }
    public string label { get; set; }
    public string image { get; set; }
    public string source { get; set; }
    public string url { get; set; }
    public string shareAs { get; set; }
    public float yield { get; set; }
    public string[] dietLabels { get; set; }
    public string[] healthLabels { get; set; }
    public string[] cautions { get; set; }
    public string[] ingredientLines { get; set; }
    public Ingredient[] ingredients { get; set; }
    public float calories { get; set; }
    public float totalWeight { get; set; }
    public float totalTime { get; set; }
    public Digest[] digest { get; set; }
  }

  public class Ingredient {
    public string text { get; set; }
    public float weight { get; set; }
    public string image { get; set; }
  }

  public class Digest {
    public string label { get; set; }
    public string tag { get; set; }
    public string schemaOrgTag { get; set; }
    public float total { get; set; }
    public bool hasRDI { get; set; }
    public float daily { get; set; }
    public string unit { get; set; }
    public Sub[] sub { get; set; }
  }

  public class Sub {
    public string label { get; set; }
    public string tag { get; set; }
    public string schemaOrgTag { get; set; }
    public float total { get; set; }
    public bool hasRDI { get; set; }
    public float daily { get; set; }
    public string unit { get; set; }
  }

  }
}

// This is my deserialization code:
using (WebClient client = new WebClient()) {
  string json = client.DownloadString(strurl);
  var example = new JavaScriptSerializer().Deserialize<Class1.Rootobject>(json);
  TextBox1.Text = example.hits[0].recipe.label.ToString();
}

The error I get is: The index is outside of the bounds of the array. I have tried changing code to: var example = new JavaScriptSerializer().Deserialize<Class1.hit>(json); but this does not work either.

Json file:

{ "q": "fish", "from": 0, "to": 1, "more": true, "count": 15241, "hits": [ { "recipe": { "uri": "http://www.edamam.com/ontologies/edamam.owl#recipe_672c9e7e3fbc6240477d99152ba8f6b3", "label": "Burnt-Scallion Fish", "image": "https://www.edamam.com/web-img/a96/a967fbe797803dbe8418a668cf304b53.jpg", "source": "Saveur", "url": "http://www.saveur.com/article/Recipes/Burnt-Scallion-Fish", "shareAs": "http://www.edamam.com/recipe/burnt-scallion-fish-672c9e7e3fbc6240477d99152ba8f6b3/fish", "yield": 6.0, "dietLabels": [ "Low-Carb" ], "healthLabels": [ "Sugar-Conscious", "Peanut-Free", "Tree-Nut-Free", "Alcohol-Free", "Immuno-Supportive" ], "cautions": [], "ingredientLines": [ "2 bunches scallions", "8 tbsp. butter", "2 8-oz. fish filets" ], "ingredients": [ { "text": "2 bunches scallions", "weight": 226.0, "image": "https://www.edamam.com/food-img/b89/b89986ed6aa466285bdd99bac34b3c46.jpg" }, { "text": "8 tbsp. butter", "weight": 113.6, "image": null }, { "text": "2 8-oz. fish filets", "weight": 453.59237, "image": "https://www.edamam.com/food-img/717/717cb400eb49626bb7c95cd29292cef4.jpg" } ], "calories": 1322.2806752000001, "totalWeight": 793.19237, "totalTime": 0.0, "totalNutrients": { "ENERC_KCAL": { "label": "Energy", "quantity": 1322.2806752000001, "unit": "kcal" }, "FAT": { "label": "Fat", "quantity": 100.28143028999999, "unit": "g" }, "FASAT": { "label": "Saturated", "quantity": 61.0798833645, "unit": "g" }, "FATRN": { "label": "Trans", "quantity": 3.7238079999999996, "unit": "g" }, "FAMS": { "label": "Monounsaturated", "quantity": 26.1997660026, "unit": "g" }, "FAPU": { "label": "Polyunsaturated", "quantity": 5.2706283031000005, "unit": "g" }, "CHOCDF": { "label": "Carbs", "quantity": 16.65656, "unit": "g" }, "FIBTG": { "label": "Fiber", "quantity": 5.876000000000001, "unit": "g" }, "SUGAR": { "label": "Sugars", "quantity": 5.33396, "unit": "g" }, "PROCNT": { "label": "Protein", "quantity": 96.182747896, "unit": "g" }, "CHOLE": { "label": "Cholesterol", "quantity": 471.03618500000005, "unit": "mg" }, "NA": { "label": "Sodium", "quantity": 284.5240324, "unit": "mg" }, "CA": { "label": "Calcium", "quantity": 235.34323700000004, "unit": "mg" }, "MG": { "label": "Magnesium", "quantity": 169.94193990000002, "unit": "mg" }, "K": { "label": "Potassium", "quantity": 2020.8729574000004, "unit": "mg" }, "FE": { "label": "Iron", "quantity": 5.9076372720000006, "unit": "mg" }, "ZN": { "label": "Zinc", "quantity": 2.4804948210000006, "unit": "mg" }, "P": { "label": "Phosphorus", "quantity": 881.9910290000001, "unit": "mg" }, "VITA_RAE": { "label": "Vitamin A", "quantity": 890.0239999999999, "unit": "µg" }, "VITC": { "label": "Vitamin C", "quantity": 42.48800000000001, "unit": "mg" }, "THIA": { "label": "Thiamin (B1)", "quantity": 0.3159528717000001, "unit": "mg" }, "RIBF": { "label": "Riboflavin (B2)", "quantity": 0.5051871931, "unit": "mg" }, "NIA": { "label": "Niacin (B3)", "quantity": 18.9379222011, "unit": "mg" }, "VITB6A": { "label": "Vitamin B6", "quantity": 0.8760876394000001, "unit": "mg" }, "FOLDFE": { "label": "Folate equivalent (total)", "quantity": 256.9101688, "unit": "µg" }, "FOLFD": { "label": "Folate (food)", "quantity": 256.9101688, "unit": "µg" }, "FOLAC": { "label": "Folic acid", "quantity": 0.0, "unit": "µg" }, "VITB12": { "label": "Vitamin B12", "quantity": 7.359879446000002, "unit": "µg" }, "VITD": { "label": "Vitamin D", "quantity": 15.76536347, "unit": "µg" }, "TOCPHA": { "label": "Vitamin E", "quantity": 5.69288948, "unit": "mg" }, "VITK1": { "label": "Vitamin K", "quantity": 482.12229318000004, "unit": "µg" }, "WATER": { "label": "Water", "quantity": 577.5605624960001, "unit": "g" } }, "totalDaily": { "ENERC_KCAL": { "label": "Energy", "quantity": 66.11403376000001, "unit": "%" }, "FAT": { "label": "Fat", "quantity": 154.27912352307692, "unit": "%" }, "FASAT": { "label": "Saturated", "quantity": 305.39941682250003, "unit": "%" }, "CHOCDF": { "label": "Carbs", "quantity": 5.552186666666667, "unit": "%" }, "FIBTG": { "label": "Fiber", "quantity": 23.504000000000005, "unit": "%" }, "PROCNT": { "label": "Protein", "quantity": 192.365495792, "unit": "%" }, "CHOLE": { "label": "Cholesterol", "quantity": 157.01206166666668, "unit": "%" }, "NA": { "label": "Sodium", "quantity": 11.855168016666667, "unit": "%" }, "CA": { "label": "Calcium", "quantity": 23.534323700000005, "unit": "%" }, "MG": { "label": "Magnesium", "quantity": 40.46236664285715, "unit": "%" }, "K": { "label": "Potassium", "quantity": 42.99729696595745, "unit": "%" }, "FE": { "label": "Iron", "quantity": 32.82020706666667, "unit": "%" }, "ZN": { "label": "Zinc", "quantity": 22.549952918181823, "unit": "%" }, "P": { "label": "Phosphorus", "quantity": 125.99871842857145, "unit": "%" }, "VITA_RAE": { "label": "Vitamin A", "quantity": 98.89155555555556, "unit": "%" }, "VITC": { "label": "Vitamin C", "quantity": 47.2088888888889, "unit": "%" }, "THIA": { "label": "Thiamin (B1)", "quantity": 26.329405975000007, "unit": "%" }, "RIBF": { "label": "Riboflavin (B2)", "quantity": 38.860553315384614, "unit": "%" }, "NIA": { "label": "Niacin (B3)", "quantity": 118.362013756875, "unit": "%" }, "VITB6A": { "label": "Vitamin B6", "quantity": 67.39135687692308, "unit": "%" }, "FOLDFE": { "label": "Folate equivalent (total)", "quantity": 64.2275422, "unit": "%" }, "VITB12": { "label": "Vitamin B12", "quantity": 306.66164358333344, "unit": "%" }, "VITD": { "label": "Vitamin D", "quantity": 105.10242313333333, "unit": "%" }, "TOCPHA": { "label": "Vitamin E", "quantity": 37.952596533333335, "unit": "%" }, "VITK1": { "label": "Vitamin K", "quantity": 401.76857765000005, "unit": "%" } }, "digest": [ { "label": "Fat", "tag": "FAT", "schemaOrgTag": "fatContent", "total": 100.28143028999999, "hasRDI": true, "daily": 154.27912352307692, "unit": "g", "sub": [ { "label": "Saturated", "tag": "FASAT", "schemaOrgTag": "saturatedFatContent", "total": 61.0798833645, "hasRDI": true, "daily": 305.39941682250003, "unit": "g" }, { "label": "Trans", "tag": "FATRN", "schemaOrgTag": "transFatContent", "total": 3.7238079999999996, "hasRDI": false, "daily": 0.0, "unit": "g" }, { "label": "Monounsaturated", "tag": "FAMS", "schemaOrgTag": null, "total": 26.1997660026, "hasRDI": false, "daily": 0.0, "unit": "g" }, { "label": "Polyunsaturated", "tag": "FAPU", "schemaOrgTag": null, "total": 5.2706283031000005, "hasRDI": false, "daily": 0.0, "unit": "g" } ] }, { "label": "Carbs", "tag": "CHOCDF", "schemaOrgTag": "carbohydrateContent", "total": 16.65656, "hasRDI": true, "daily": 5.552186666666667, "unit": "g", "sub": [ { "label": "Carbs (net)", "tag": "CHOCDF.net", "schemaOrgTag": null, "total": 10.780559999999998, "hasRDI": false, "daily": 0.0, "unit": "g" }, { "label": "Fiber", "tag": "FIBTG", "schemaOrgTag": "fiberContent", "total": 5.876000000000001, "hasRDI": true, "daily": 23.504000000000005, "unit": "g" }, { "label": "Sugars", "tag": "SUGAR", "schemaOrgTag": "sugarContent", "total": 5.33396, "hasRDI": false, "daily": 0.0, "unit": "g" }, { "label": "Sugars, added", "tag": "SUGAR.added", "schemaOrgTag": null, "total": 0.0, "hasRDI": false, "daily": 0.0, "unit": "g" } ] }, { "label": "Protein", "tag": "PROCNT", "schemaOrgTag": "proteinContent", "total": 96.182747896, "hasRDI": true, "daily": 192.365495792, "unit": "g" }, { "label": "Cholesterol", "tag": "CHOLE", "schemaOrgTag": "cholesterolContent", "total": 471.03618500000005, "hasRDI": true, "daily": 157.01206166666668, "unit": "mg" }, { "label": "Sodium", "tag": "NA", "schemaOrgTag": "sodiumContent", "total": 284.5240324, "hasRDI": true, "daily": 11.855168016666667, "unit": "mg" }, { "label": "Calcium", "tag": "CA", "schemaOrgTag": null, "total": 235.34323700000004, "hasRDI": true, "daily": 23.534323700000005, "unit": "mg" }, { "label": "Magnesium", "tag": "MG", "schemaOrgTag": null, "total": 169.94193990000002, "hasRDI": true, "daily": 40.46236664285715, "unit": "mg" }, { "label": "Potassium", "tag": "K", "schemaOrgTag": null, "total": 2020.8729574000004, "hasRDI": true, "daily": 42.99729696595745, "unit": "mg" }, { "label": "Iron", "tag": "FE", "schemaOrgTag": null, "total": 5.9076372720000006, "hasRDI": true, "daily": 32.82020706666667, "unit": "mg" }, { "label": "Zinc", "tag": "ZN", "schemaOrgTag": null, "total": 2.4804948210000006, "hasRDI": true, "daily": 22.549952918181823, "unit": "mg" }, { "label": "Phosphorus", "tag": "P", "schemaOrgTag": null, "total": 881.9910290000001, "hasRDI": true, "daily": 125.99871842857145, "unit": "mg" }, { "label": "Vitamin A", "tag": "VITA_RAE", "schemaOrgTag": null, "total": 890.0239999999999, "hasRDI": true, "daily": 98.89155555555556, "unit": "µg" }, { "label": "Vitamin C", "tag": "VITC", "schemaOrgTag": null, "total": 42.48800000000001, "hasRDI": true, "daily": 47.2088888888889, "unit": "mg" }, { "label": "Thiamin (B1)", "tag": "THIA", "schemaOrgTag": null, "total": 0.3159528717000001, "hasRDI": true, "daily": 26.329405975000007, "unit": "mg" }, { "label": "Riboflavin (B2)", "tag": "RIBF", "schemaOrgTag": null, "total": 0.5051871931, "hasRDI": true, "daily": 38.860553315384614, "unit": "mg" }, { "label": "Niacin (B3)", "tag": "NIA", "schemaOrgTag": null, "total": 18.9379222011, "hasRDI": true, "daily": 118.362013756875, "unit": "mg" }, { "label": "Vitamin B6", "tag": "VITB6A", "schemaOrgTag": null, "total": 0.8760876394000001, "hasRDI": true, "daily": 67.39135687692308, "unit": "mg" }, { "label": "Folate equivalent (total)", "tag": "FOLDFE", "schemaOrgTag": null, "total": 256.9101688, "hasRDI": true, "daily": 64.2275422, "unit": "µg" }, { "label": "Folate (food)", "tag": "FOLFD", "schemaOrgTag": null, "total": 256.9101688, "hasRDI": false, "daily": 0.0, "unit": "µg" }, { "label": "Folic acid", "tag": "FOLAC", "schemaOrgTag": null, "total": 0.0, "hasRDI": false, "daily": 0.0, "unit": "µg" }, { "label": "Vitamin B12", "tag": "VITB12", "schemaOrgTag": null, "total": 7.359879446000002, "hasRDI": true, "daily": 306.66164358333344, "unit": "µg" }, { "label": "Vitamin D", "tag": "VITD", "schemaOrgTag": null, "total": 15.76536347, "hasRDI": true, "daily": 105.10242313333333, "unit": "µg" }, { "label": "Vitamin E", "tag": "TOCPHA", "schemaOrgTag": null, "total": 5.69288948, "hasRDI": true, "daily": 37.952596533333335, "unit": "mg" }, { "label": "Vitamin K", "tag": "VITK1", "schemaOrgTag": null, "total": 482.12229318000004, "hasRDI": true, "daily": 401.76857765000005, "unit": "µg" }, { "label": "Sugar alcohols", "tag": "Sugar.alcohol", "schemaOrgTag": null, "total": 0.0, "hasRDI": false, "daily": 0.0, "unit": "g" }, { "label": "Water", "tag": "WATER", "schemaOrgTag": null, "total": 577.5605624960001, "hasRDI": false, "daily": 0.0, "unit": "g" } ] }, "bookmarked": false, "bought": false } ] }

majo
  • 1
  • 2

1 Answers1

0

Try to convert your Json to C# classes with this site and then check if you got the same class structure (https://json2csharp.com) And maybe use Newtonsoft, it works perfectly every time.

I created a Console App and tested your Json file. It worked, so idk what is going wrong in your program. In your example you refer to "Class1.Rootobject", I hope you used the correct reference in your program?

Maybe debug it and check if the json is not empty there.

 string jsonPath = @"C:\temp\Stackoverflow1.txt";
 string json = File.ReadAllText(jsonPath);
 var example = new JavaScriptSerializer().Deserialize<RecipeClass.Rootobject>(json);
 string text = example.hits[0].recipe.label.ToString();
Dominic Saladin
  • 142
  • 2
  • 8