在C#中使用Newtonsoft对嵌套的JSON对象进行反序列化 [英] Deserialisation of a nested JSON object with Newtonsoft in C#

查看:637
本文介绍了在C#中使用Newtonsoft对嵌套的JSON对象进行反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我要反序列化的示例JSON字符串:

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

我似乎无法遍历JSON的对象"关键节点值.单词之前的所有内容(背景,主题,名称和scriptName)都可以.这是我的转换器

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.

推荐答案

您可以将类声明为

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

这篇关于在C#中使用Newtonsoft对嵌套的JSON对象进行反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆