JSON 到 C# 对象

JSON to C# object

我对 JSON 有疑问。

"{"status":"ok","message":"Dane klienta zostau0142y pobrane pomyu015blnie","clientData":
{"id":22,"imie":"Pppppppppp","nazwisko":"Ppppppppppppp","tel":"111111126","email":"aaa@a.pl","ulica":"Na Przyzbie","nr_budynku":"3","nr_lokalu":"41","kod_pocztowy":"02-813","miejscowosc":"Warszawa","samochod_marka":"opel","samochod_model":"vectra","subcategories":
{"6":200}}}"

这是我的 class

public class Client
    {
        public string Status { get; set; }
        public string Message { get; set; }
        public Data clientData { get; set; }
    }

    public class Data
    {
        public Dictionary<string, string> clientData { get; set; }
    }

一切基本正确,但当我调试代码时,我的代码字段 clientData 为空。

我做错了什么?

感谢帮助!

编辑:

这就是我反序列化对象的方式。

var myObject = JsonConvert.DeserializeObject<Client>(get_person);

Json

{  
   "status":"ok",
   "message":"Dane klienta zostau0142y pobrane pomyu015blnie",
   "clientData":{  
      "id":22,
      "imie":"Pppppppppp",
      "nazwisko":"Ppppppppppppp",
      "tel":"111111126",
      "email":"aaa@a.pl",
      "ulica":"Na Przyzbie",
      "nr_budynku":"3",
      "nr_lokalu":"41",
      "kod_pocztowy":"02-813",
      "miejscowosc":"Warszawa",
      "samochod_marka":"opel",
      "samochod_model":"vectra",
      "subcategories":{  
         "6":200
      }
   }
}

C#classes

public class Subcategories
{
    public int __invalid_name__6 { get; set; }
}

public class ClientData
{
    public int id { get; set; }
    public string imie { get; set; }
    public string nazwisko { get; set; }
    public string tel { get; set; }
    public string email { get; set; }
    public string ulica { get; set; }
    public string nr_budynku { get; set; }
    public string nr_lokalu { get; set; }
    public string kod_pocztowy { get; set; }
    public string miejscowosc { get; set; }
    public string samochod_marka { get; set; }
    public string samochod_model { get; set; }
    public Subcategories subcategories { get; set; }
}

public class RootObject
{
    public string status { get; set; }
    public string message { get; set; }
    public ClientData clientData { get; set; }
}

请注意,root->clientData->subcategories->6 将导致无效的 class 名称,因为 C# 中的 class 名称无法开始有一个数字。


通过黑客修复:

例如:

public class DynamicDictionary : DynamicObject
{
    private readonly Dictionary<string, object> dictionary;

    public DynamicDictionary(Dictionary<string, object> dictionary)
    {
        this.dictionary = dictionary;
    }

    public override bool TryGetMember(
        GetMemberBinder binder, out object result)
    {
        return dictionary.TryGetValue(binder.Name, out result);
    }

    public override bool TrySetMember(
        SetMemberBinder binder, object value)
    {
        dictionary[binder.Name] = value;

        return true;
    }
}

可以这样使用:

dynamic x = new DynamicDictionary(
    new Dictionary<string, object> {{"Name", "Peter"}});

您当前尝试的问题是您正在尝试将 clientData 转换为 Dictionary<string, string>。这导致了一个问题,因为并非所有值都是字符串,有问题的如下:

id : int
subcategories : Dictionary<string, int>

如果您不想明确定义所有属性,因为它们会在不通知的情况下发生更改,那么我建议您对 JSON 结构进行如下更改:

{
    "status": "ok",
    "message": "Dane klienta zostau0142y pobrane pomyu015blnie",
    "clientData": {
        "id": 22,
        "properties": {
            "imie": "Pppppppppp",
            "nazwisko": "Ppppppppppppp",
            "tel": "111111126",
            "email": "aaa@a.pl",
            "ulica": "Na Przyzbie",
            "nr_budynku": "3",
            "nr_lokalu": "41",
            "kod_pocztowy": "02-813",
            "miejscowosc": "Warszawa",
            "samochod_marka": "opel",
            "samochod_model": "vectra"
        },
        "subcategories": {
            "6": 200
        }
    }
}

然后将 C# class 结构更改为以下内容:

public class Client
{
    public string Status { get; set; }
    public string Message { get; set; }
    public Data clientData { get; set; }
}

public class Data
{
    public int id { get; set;}
    public Dictionary<string, string> properties { get; set; }
    public Dictionary<string, int> subcategories { get; set; }
}

这应该有效(虽然我还没有测试过),并且希望能让你按照你需要的方式使用它。

注意:您还可以将 idsubcategories 移动到根目录中,并将 clientData 保留为 Dictionary<string, string> .一切都取决于你的喜好,这里重要的是你要小心不要混合类型。

您可以使用 Newtonsoft.Json - 添加对您的项目的引用并添加 using 指令

使用Newtonsoft.Json;

        //then your code 
        dynamic ma_json = JsonConvert.DeserializeObject<dynamic>(json);
        //and then you can get say the id:
        var id = ma_json.clientData.id; 
        // ... do whatever you want with the id
        if (ma_json.clientData.id == 22) //evaluates to true in your case
        { 
           //do something
        }