如何反序列化json? [英] How to deserialize json?

查看:119
本文介绍了如何反序列化json?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我在c#类中遇到了json反序列化的问题。 Json将通过api返回: -

Hello,
I am facing a problem with json deserialization in c# class. The Json will return by api is :-

{
    "fields": [
        {
            "value": "3732",
            "order": 1,
            "name": "id",
            "type": "hidden"
        },
        {
            "maxlen": "13",
            "required": "",
            "value": "0",
            "order": 2,
            "name": "job",
            "label": "Job ",
            "type": "text"
        },
        {
            "maxlen": "13",
            "required": "",
            "value": "0",
            "order": 3,
            "name": "client",
            "label": "Client ",
            "type": "text"
        },
        {
            "maxlen": "50",
            "required": "required",
            "value": "Tim Nguyen",
            "order": 4,
            "name": "task_name",
            "label": "Task Name ",
            "type": "text"
        },
        {
            "required": "required",
            "time": "05:30 PM",
            "date": "07/18/2017",
            "order": 5,
            "name": "start_date",
            "label": "Start Date ",
            "type": "datetime"
        },
        {
            "required": "",
            "time": "07:00 PM",
            "date": "07/18/2017",
            "order": 6,
            "name": "end_date",
            "label": "End Date ",
            "type": "datetime"
        },
        {
            "required": "",
            "value": "Roof Inspection",
            "order": 7,
            "name": "task_description",
            "label": "Task Description ",
            "type": "textareainput"
        }
    ],
    "action": "update_task",
    "hidden": {},
    "display": "Update Task"
}





请帮我解决这个问题。



Regard

Ankush



我尝试了什么:





Please help me to solve this.

Regard
Ankush

What I have tried:

public class FormFillByTaskId
{
	public FieldData[] fields { get; set; }
}
public class FieldData
{
   // Don't know how to deserialize over here..
}

推荐答案

有些网站会根据JSON数据为您生成类。我最喜欢的是: JSON Utils:从JSON生成C#,VB.Net,SQL表,Java和PHP [ ^ ]



使用上面的JSON数据,以下类是生成:

There are websites that will generate classes for you from JSON data. My favourite is: JSON Utils: Generate C#, VB.Net, SQL Table, Java and PHP from JSON[^]

Using the above JSON data, the following classes are generated:
public class Field
{

    [JsonProperty("value")]
    public string Value { get; set; }

    [JsonProperty("order")]
    public int Order { get; set; }

    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("type")]
    public string Type { get; set; }

    [JsonProperty("maxlen")]
    public string Maxlen { get; set; }

    [JsonProperty("required")]
    public string Required { get; set; }

    [JsonProperty("label")]
    public string Label { get; set; }

    [JsonProperty("time")]
    public string Time { get; set; }

    [JsonProperty("date")]
    public string Date { get; set; }
}

public class Hidden
{
}

public class Example
{

    [JsonProperty("fields")]
    public IList<Field> Fields { get; set; }

    [JsonProperty("action")]
    public string Action { get; set; }

    [JsonProperty("hidden")]
    public Hidden Hidden { get; set; }

    [JsonProperty("display")]
    public string Display { get; set; }
}



接下来,您需要编写代码以将JSON数据转换/映射到类结构。 NuGet Gallery | Json.NET 10.0.3 [ ^ 大部分都使用了。



一旦在你的项目中被引用,这里有一个帮助类,用于从/到类的转换:


Next you will need to write code to convert / map the JSON data to the class structure. NuGet Gallery | Json.NET 10.0.3[^] is used by most.

Once referenced in your project, here is a helper class that I use to do the conversion from/to classes:

public static class JsonConverter
{
    public static string FromClass<T>(T data, bool isEmptyToNull = false, JsonSerializerSettings jsonSettings = null)
    {
        string response = string.Empty;

        if (!EqualityComparer<T>.Default.Equals(data, default(T)))
            response = JsonConvert.SerializeObject(data, jsonSettings);

        return isEmptyToNull ? (response == "{}" ? "null" : response) : response;
    }

    public static T ToClass<T>(string data, JsonSerializerSettings jsonSettings = null)
    {
        var response = default(T);

        if (!string.IsNullOrEmpty(data))
            response = jsonSettings == null
                ? JsonConvert.DeserializeObject<T>(data)
                : JsonConvert.DeserializeObject<T>(data, jsonSettings);

        return response;
    }
}



这是如何使用帮助程序类:


And this is how to use the helper class:

var rawJson = "{\"fields\": [{\"value\": \"3732\",\"order\": 1,\"name\": \"id\",\"type\": \"hidden\"},{\"maxlen\": \"13\",\"required\": \"\",\"value\": \"0\",\"order\": 2,\"name\": \"job\",\"label\": \"Job \",\"type\": \"text\"},{\"maxlen\": \"13\",\"required\": \"\",\"value\": \"0\",\"order\": 3,\"name\": \"client\",\"label\": \"Client \",\"type\": \"text\"},{\"maxlen\": \"50\",\"required\": \"required\",\"value\": \"Tim Nguyen\",\"order\": 4,\"name\": \"task_name\",\"label\": \"Task Name \",\"type\": \"text\"},{\"required\": \"required\",\"time\": \"05:30 PM\",\"date\": \"07/18/2017\",\"order\": 5,\"name\": \"start_date\",\"label\": \"Start Date \",\"type\": \"datetime\"},{\"required\": \"\",\"time\": \"07:00 PM\",\"date\": \"07/18/2017\",\"order\": 6,\"name\": \"end_date\",\"label\": \"End Date \",\"type\": \"datetime\"},{\"required\": \"\",\"value\": \"Roof Inspection\",\"order\": 7,\"name\": \"task_description\",\"label\": \"Task Description \",\"type\": \"textareainput\"}],\"action\": \"update_task\",\"hidden\": {},\"display\": \"Update Task\"}";
var result = JsonConverter.ToClass<Example>(rawJson);


这篇关于如何反序列化json?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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