JSON to C# object

本文关键字:object to JSON | 更新日期: 2023-09-27 18:36:29

我对 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}}}"

这是我的班级

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

一切都基本正确,但是当我调试我的代码字段客户端数据时,数据为空。

我做错了什么?

感谢您的帮助!

编辑:

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

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

JSON to C# object

当前尝试的问题在于您正在尝试将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# 类结构更改为以下内容:

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>。一切都取决于您的喜好,这里重要的是您注意不要混合类型。

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# 类

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 将导致类名无效,因为 C# 中的类名不能以数字开头。


通过黑客修复:

例如:

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

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

使用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
        }