0

I am attempting to get ONLY the FIRST property (params) from the JSON object below and store it as a struct in C#. I was using the solution on this page. But I was not successful. If anyone could help or point me in the right direction it would be very helpful. Thank you.

{
"params" : [
    {"OneInstance" : "true"},
    {"Single" : "3"},
    {"File_Name" : "test.exe"},
    {"Conf_Name" : "inst.bin"}
],
"directories" : [
    {"file1": "C:/Program Files (x86)/whatever/example"},
    {"file2": "C:/Program Files/another/example"}
]}

public static JValuesStruct paramValues;
StreamReader streamReader = new StreamReader(@"Sample.json");
        using (JsonTextReader reader = new JsonTextReader(streamReader))
        {
            while (reader.Read())
            {
                if (reader.TokenType == JsonToken.StartObject)
                {
                    // Load each object from the stream and do something with it

                    JObject obj = JObject.Load(reader);

                    JsonSerializer serializer = new JsonSerializer();
                    paramValues = (JValuesStruct)serializer.Deserialize(new JTokenReader(obj), typeof(JValuesStruct));
                }
            }
        }


public struct JValuesStruct
{
    public bool OneInstance { get; set; }
    public string Single { get; set; }
    public string File_Name { get; set; }
    public string Conf_Name { get; set; }
}
Athanasios Emmanouilidis
  • 2,084
  • 3
  • 18
  • 30
Archon
  • 75
  • 1
  • 8
  • 2
    It would be awesome if you could provide a [mcve] of your attempt so far. – mjwills Jun 12 '18 at 21:53
  • Is there an error? it looks like you're missing a variable name for the output of the `Deserialize` call, unless you're naming a property the same name as a struct (bad practice, BTW). – Heretic Monkey Jun 12 '18 at 22:13
  • There is no syntax error when I run this. It just gives a blank output. Also just made a slight change. Before the struct declaration was missing. – Archon Jun 12 '18 at 22:15
  • The first json object ? It is only one json object. Do you mean the first property (params)? – Athanasios Emmanouilidis Jun 12 '18 at 22:19
  • Yes, the first property (params), and preemptive to any future comments, I am new to working with JSON. Obviously I am going to clean this up later, I would never post finished code online publicly. I am just quickly trying to get this to work. Then I will go back and perform cleanup. – Archon Jun 12 '18 at 22:34

1 Answers1

0

Here you go. It works perfect. First download "newtonsoft.json" from Nuget and add it to your project. Struct myStruct is your deserialized json object converted to a struct.

public void DeserializeMyJsonObject()
{
    string json;
    using (StreamReader r = new StreamReader("d:\\json.txt"))
    {
        json = r.ReadToEnd();
    }

    var myStruct = JsonConvert.DeserializeObject<YourStruct>(json);

}

public struct YourStruct
{
    [JsonProperty("params")]
    public List<Object> KeyAndProperties { get; set; }
}
Athanasios Emmanouilidis
  • 2,084
  • 3
  • 18
  • 30