简体   繁体   English

JSON到C#对象

[英]JSON to C# object

I have a problem with JSON. 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}}}"

and it's my 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; }
    }

everything is mostly correct but when I debug my code field clientData is null. 一切基本上都是正确的,但是当我调试我的代码字段时,clientData为null。

What am I doing wrong? 我究竟做错了什么?

Thanks for help! 感谢帮助!

EDIT: 编辑:

it's how I deserialize object. 这就是我反序列化对象的方式。

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

The problem with your current attempt is that you are trying to convert clientData to a Dictionary<string, string> . 当前尝试的问题是您正在尝试将clientData转换为Dictionary<string, string> This is causing an issue because not all of your values are strings, the problematic ones are as follows: 这引起了一个问题,因为并非所有值都是字符串,有问题的值如下所示:

id : int
subcategories : Dictionary<string, int>

If you don't want to explicitly define all of your properties due to them changing without notice, then I would recommend a change to your JSON structure as follows: 如果您不希望因更改属性而明确定义所有属性,而无需另行通知,那么我建议对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
        }
    }
}

Then you change your C# class structure to the following: 然后,将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; }
}

That should work (though I haven't tested), and will hopefully allow you to use it how you need to still. 那应该工作(尽管我还没有测试),并且希望可以让您按需使用它。

NOTE : You could also move id and subcategories into the root, and keep clientData as a Dictionary<string, string> . 注意 :您还可以将idsubcategories移到根目录,并将clientData保留为Dictionary<string, string> All depends on your preference really, the important thing here is that you be careful not to mix types. 一切都取决于您的偏好,这里重要的是您要小心不要混合类型。

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

Note that root->clientData->subcategories->6 would result in invalid class name, as class names in C# can not begin with a number. 请注意, root-> clientData-> subcategories-> 6将导致无效的类名,因为C#中的类名不能以数字开头。


With hack fix: 使用hack修复:

For example: 例如:

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

Which can be used as follows: 可以如下使用:

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

you can use Newtonsoft.Json - add a reference to your project and add the using directive 您可以使用Newtonsoft.Json-添加对项目的引用并添加using指令

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM