0

I'm a newbie having my first go at importing data from a json file to a c# application. In this case, I'm making an app to organise and manage recipes for a crafting videogame I'm playing.

I have a json file with my recipe info in it;

{
    "assembler_recipes":[
        {
            "ItemProduced":"AI_Limiter",
            "ProductionCount":5,
            "Resources":{
                "iron_Plate":11.25,
                "rubber":3.75
            },
            "Byproducts":{
                
            }
        },
        {
            "ItemProduced":"alclad_Aluminium_Sheet",
            "ProductionCount":30,
            "Resources":{
                "aluminium_Ingot":30,
                "copper_Ingot":10
            },
            "Byproducts":{
                
            }
        }, // etc...
    ]
}

and the format I want it to be in;

 public class Recipe
{ 
    public KeyValuePair<Items, decimal> Produces { get; set; }
    public Dictionary<Items,decimal> Resources { get; set; }
    public Dictionary<Items, decimal> Byproducts { get; set; }

}

This is my method to import it;

public class Recipe_List
{
    public Recipe_List()
    {
        var dataFile = File.ReadAllText("C:\\Users\\drumk\\source\\repos\\Satisfactory_Factory_Planner\\Satisfactory_Objects\\Recipes\\satisfactory_recipes.json");
        //Console.WriteLine(dataFile);

        var JSONdata = JsonSerializer.Deserialize<List<Recipe>>(dataFile);

        foreach(Recipe recipe in JSONdata)
        {
            Console.WriteLine(recipe);
        }
    }
}

The data is being imported because if I use Console.WriteLine(dataFile); it prints it to the Console perfectly. But the Deserialize method is just returning "Satisfactory_Objects.Recipes.Recipe", not the data stored in it.

What am I doing wrong?

dbc
  • 104,963
  • 20
  • 228
  • 340
  • 1
    Well, for starters your JSON string is not an array, its an object with an array value. Furthermore the internal array does not match your `Recipe` class. – freakish Aug 11 '22 at 07:51
  • 2
    Because `Console.WriteLine(recipe)` will simply call `ToString` on `recipe`, and since your `Recipe` class doesn't override the `ToString` method it will use the default behaivour, which is to just return the full type name, i.e `Satisfactory_Objects.Recipes.Recipe` so working as intended. What do you actually want to print? – MindSwipe Aug 11 '22 at 08:01
  • 2
    Furhermore, deserializing this won't work. I recommend you use a tool like [json2csharp](https://json2csharp.com/) (remember to tick the "Use Pascal Case" setting) to generate a rough outline of classes you need – MindSwipe Aug 11 '22 at 08:05
  • 1
    As @freakish points out, your class doesn't match the json. For example, 'Produces' is not in the json and is expecting a key value pair. I'm assuming that should be 'ItemProduced' and be expecting a string. Same sort of thing goes for Resources and ByProducts in that the type they're expecting is not the same as what's in the json. – sr28 Aug 11 '22 at 08:07
  • Thank you guys, I'll go back over it and check the formatting again. Like I say it's my first time using JSON.. Freakish when you say it's not an array, does that mean I should enclose it all in [] ? MindSwipe what I'm trying to do is have a recipe structure that the data can be inserted into and then processed from there. Thanks for the link I'll check that out now – Drumknott88 Aug 11 '22 at 08:32

1 Answers1

0
internal class Composition
{
    public string ItemProduced { get; set; }

    public int ProductionCount { get; set; }

    public Dictionary<string, decimal> Resources { get; set; }

    public Dictionary<string, decimal> Products { get; set; }

}

public class Recipe
{
    [JsonProperty("assembler_recipes")]
    IEnumerable<Composition> Compositions { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        var json = File.ReadAllText(@"c:\data.json");
        var recipe = JsonConvert.DeserializeObject<Recipe>(json);
        Console.ReadKey();
    }
}

Happy Coding

jay rao
  • 1
  • 1
  • 1
    `JsonConvert` comes from Newtonsoft.Json – Alexander Petrov Aug 11 '22 at 12:37
  • 2
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 15 '22 at 17:13