c# - 反序列化 JSON Objest 导致所有空字段 C#

标签 c# json serialization

我的 jsonResponse 是这样的:

{"status":200,"data":{"first_name":"\u062e\u0633","last_name":"\u0635\u062f\u0627","national_code":"1","image_photo":"1.jpg","cellphone":"1234","city":{"id":1,"name":"x","created_at":"2017-02-27 17:54:44","updated_at":"2017-02-27 17:54:44"},"email":"something@gmail.com","even_odd":1,"Register Time":"2018-01-25 10:39:17","is_blocked":false,"receive_regular_offer":"false","level":1,"ride_count":0,"service_type":1,"bank":"\u0645","iban":"xy","card_number":"","holder":"\u062e\u0633","plate_number":"123","vehicle_model":"\u067e\u0698","vehicle_color":"\u062a\u0627\u06a9\u0633","unique_id":592875}}

我创建了这样一个类:

public class Driver
    {
        public string first_name { get; set; }
        public string last_name { get; set; }
        public string national_code { get; set; }
        public string image_photo { get; set; }
        public string cellphone { get; set; }
        public string city { get; set; }
        public string email { get; set; }
        public string even_odd { get; set; }
        public bool is_blocked { get; set; }
        public bool receive_regular_offer { get; set; }
        public string level { get; set; }
        public string ride_count { get; set; }
        public string service_type { get; set; }
        public string bank { get; set; }
        public string iban { get; set; }
        public string card_number { get; set; }
        public string holder { get; set; }
        public string vehicle_model { get; set; }
        public string vehicle_color { get; set; }
        public string unique_id { get; set; }
    }

并使用了这个:

jsonResponse = reader.ReadToEnd();
JavaScriptSerializer js = new JavaScriptSerializer();
Driver snappDriver = js.Deserialize<Driver>(jsonResponse);

但结果全为null!

最佳答案

1.你的类应该定义正确

例子:

void Main()
{
    var json =api();

    //dynamic
    var dynamic_json = JsonConvert.DeserializeObject(json).Dump() as JObject;

    //strong type
    var strong_Type_json = JsonConvert.DeserializeObject<Driver>(json).Dump() ;

}

string api(){
    return @"
{""status"":200,""data"":{""first_name"":""\u062e\u0633"",""last_name"":""\u0635\u062f\u0627"",""national_code"":""1"",""image_photo"":""1.jpg"",""cellphone"":""1234"",""city"":{""id"":1,""name"":""x"",""created_at"":""2017-02-27 17:54:44"",""updated_at"":""2017-02-27 17:54:44""},""email"":""something@gmail.com"",""even_odd"":1,""Register_Time"":""2018-01-25 10:39:17"",""is_blocked"":false,""receive_regular_offer"":""false"",""level"":1,""ride_count"":0,""service_type"":1,""bank"":""\u0645"",""iban"":""xy"",""card_number"":"""",""holder"":""\u062e\u0633"",""plate_number"":""123"",""vehicle_model"":""\u067e\u0698"",""vehicle_color"":""\u062a\u0627\u06a9\u0633"",""unique_id"":592875}}
    ";
}


public class City
{
    public int id { get; set; }
    public string name { get; set; }
    public string created_at { get; set; }
    public string updated_at { get; set; }
}

public class Data
{
    public string first_name { get; set; }
    public string last_name { get; set; }
    public string national_code { get; set; }
    public string image_photo { get; set; }
    public string cellphone { get; set; }
    public City city { get; set; }
    public string email { get; set; }
    public int even_odd { get; set; }
    public string Register_Time { get; set; }
    public bool is_blocked { get; set; }
    public string receive_regular_offer { get; set; }
    public int level { get; set; }
    public int ride_count { get; set; }
    public int service_type { get; set; }
    public string bank { get; set; }
    public string iban { get; set; }
    public string card_number { get; set; }
    public string holder { get; set; }
    public string plate_number { get; set; }
    public string vehicle_model { get; set; }
    public string vehicle_color { get; set; }
    public int unique_id { get; set; }
}

public class Driver
{
    public int status { get; set; }
    public Data data { get; set; }
}

2.json's key Register Time in strong type 是无效名称

你可以在你的json字符串中添加_来解决问题

关于c# - 反序列化 JSON Objest 导致所有空字段 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50074319/

相关文章:

c# - 使用 xsd.exe 反序列化 - 如何反序列化为对象,而不是数据集?

WPF 加载序列化图像

android - 如何按 Intent 传递对象

javascript - 如何将类型对象的 json 数组传递给 MVC Controller 类

c# - 如何获取包含字符串的列表的索引

c# - Insert语句与外键fk_student冲突,冲突数据库student中的 "id"列

php - 将多行 MySQL 数据库返回到 iOS App

c# - .NET Core System.Drawing.Common PrintDocument 在 Linux 上不起作用

javascript - 使用 lodash 方法返回新形式的数组

java - 使用 Jackson 忽略 JSON 对象上的新字段