5

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.

Community
  • 1
  • 1
Dramos
  • 73
  • 1
  • 2
  • 5
  • Really? All your JSON data there in one loooooooooooong line in your question? Who should read that? –  Jun 30 '14 at 18:59
  • 1
    Edited your question and formatted the JSON data block in a more readable manner... –  Jun 30 '14 at 19:05
  • Just to explain what causes your problem (go with L.B.'s answer as a solution): Note that you already have deserialized the root JSON object and all its inner objects (that includes also the "objects" collection) with the first call of serializer.Deserialize. When the method returns, the readers position is at the end of the JSON data. The second call of serializer.Deserialize will thus not provide you any data anymore... –  Jun 30 '14 at 19:29

1 Answers1

12

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);
}
L.B
  • 114,136
  • 19
  • 178
  • 224
  • Thanks for the input, I used to do this and it works fine but ideally, I would want to integrate the JSON converter because I want to transform Position values (top,left) into a custom Region object containing those two fractions. – Dramos Jun 30 '14 at 19:25
  • 2
    @Dramos What do you expect me to do without knowing what your custom JsonConverter does and what your Region object is? – L.B Jun 30 '14 at 19:33
  • It's alright, sorry for the misleading question. I just wanted to understand why I couldn't iterate through my "objects" node inside the converter. elgonzo gave me the answer ("The readers position is at the end of the JSON data. Hence, will not read and provide data again.") – Dramos Jun 30 '14 at 20:22