Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserialisation of a nested JSON object with Newtonsoft in C# [closed]

Tags:

json

c#

json.net

Here is a sample JSON string that I'm trying to deserialize:

{
    "background": "#ededf0",
    "theme": "Flat",
    "name": "Control Page Name",
    "scriptName": "Control Page Script",
    "objects": [{
        "ID": "76799598",
        "position": {
            "top": 0.30428571428571427,
            "left": 0.6054421768707483
        },
        "width": 0.09451596023024594,
        "height": 0.07450128571428571,
        "label": "btn",
        "colour": "blue",
        "action": "OpenLayout",
        "actionParams": {
            "actionName": "Explorer",
            "itemId": "91d5bfff-a723-498a-a846-24a9e41fbaa6",
            "groupId": "bf434b0d-90c2-496a-96ce-c09f228255b4"
        }
    }]
}

I can't seem to iterate through the "objects" key node value of the JSON. Everything before words fine (background, theme,name and scriptName). Here's my converter

{
        JObject panel = (JObject)serializer.Deserialize(reader);

        var cpResponse = new VWControlPanelResponse();

        JToken scriptGroupId, scriptId;
        cpResponse.Background = (string)((JValue)panel["background"]).Value;
        cpResponse.Theme = (string)((JValue)panel["theme"]).Value;
        cpResponse.Name = (string)((JValue)panel["name"]).Value;
        cpResponse.ScriptName = (string)((JValue)panel["scriptName"]).Value;
        cpResponse.ScriptGroupId = panel.TryGetValue("scriptGroupId", out scriptGroupId) ? new Guid(scriptGroupId.ToString()) : Guid.Empty;
        cpResponse.ScriptId = panel.TryGetValue("scriptId", out scriptId) ? new Guid(scriptId.ToString()) : Guid.Empty;

        cpResponse.VisualElements = serializer.Deserialize<List<VisualElement>>(reader);


        return cpResponse;
    }

... and here are my models:

[JsonConverter(typeof(Converter))]
public class Response
{
    public string Background { get; set; }
    public string Theme { get; set; }
    public string Name { get; set; }
    public string ScriptName { get; set; }

    public Guid ScriptGroupId { get; set; }
    public Guid ScriptId { get; set; }

    public List<VisualElement> VisualElements { get; set; }

}

public class VisualElement
{
    public long ID { get; set; }
}

}

I've went through many similar articles online (this one in particular JSON Deserialization C# but I can't seem to figure out why it doesn't want to translate my VisualElements node. I tried using reader.Read() actions but the reader's token type indicates that it's an end of object.

like image 724
Dramos Avatar asked Dec 19 '22 13:12

Dramos


1 Answers

You can declare your classes as

public class Position
{
    public double top { get; set; }
    public double left { get; set; }
}

public class ActionParams
{
    public string actionName { get; set; }
    public string itemId { get; set; }
    public string groupId { get; set; }
}

public class VisualElement
{
    public string ID { get; set; }
    public Position position { get; set; }
    public double width { get; set; }
    public double height { get; set; }
    public string label { get; set; }
    public string colour { get; set; }
    public string action { get; set; }
    public ActionParams actionParams { get; set; }
}

public class Response
{
    public string background { get; set; }
    public string theme { get; set; }
    public string name { get; set; }
    public string scriptName { get; set; }
    public List<VisualElement> objects { get; set; }
}

deserialize as

 var response = JsonConvert.DeserializeObject<Response>(json);

and use as

foreach (var vObj in response.objects)
{
    Console.WriteLine(vObj.colour);
}
like image 150
L.B Avatar answered Dec 24 '22 01:12

L.B