使用C#中的Newtonsoft反序列化嵌套的JSON对象

时间:2014-06-30 18:51:55

标签: c# json json.net

以下是我尝试反序列化的示例JSON字符串:

{
    "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"
        }
    }]
}

我似乎无法遍历"对象" JSON的关键节点值。之前的一切都很好(背景,主题,名称和scriptName)。这是我的转换器

{
        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;
    }

......这是我的模特:

[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; }
}

}

我在网上浏览了很多类似的文章(尤其是JSON Deserialization C#,但我似乎无法弄清楚为什么它不想翻译我的VisualElements节点。我尝试使用reader.Read()动作,但读者的令牌类型表明它是对象的结束。

1 个答案:

答案 0 :(得分:4)

您可以将课程声明为

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; }
}

反序列化为

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

并用作

foreach (var vObj in response.objects)
{
    Console.WriteLine(vObj.colour);
}