1

任何人都可以帮助为我的 JSON 格式编写 Newtonsoft JSON Deserializer。

我有以下 json 格式。

{
    "success": true,
    "message": "Missing ajax operation. Please contact administrator.",
    "data": {
        "mode": "new",
        "data": {
            "1": {
                "CustomerCode": "CUST00001",
                "Name": "Dell Asia Pacific Sdn. Bhd.",
                "Add1": "Plot 76 Mukim 11, Bukit Tengah Industrial Park; Bukit Mertajam; Pulau Pinang; 14000",
                "Add2": "",
                "Add3": "",
                "Daddr1": "Level 21, Suite 21.01, The Gardens South Tower, Mid Valley City, Lingkaran Syed Putra, ; Kuala Lumpur; Wilayah Persekutuan; 59200",
                "Daddr2": "",
                "Daddr3": "",
                "Attn": "",
                "Phone": "",
                "PhoneA": "",
                "Fax": "",
                "Area": "",
                "Agent": "",
                "Email": "",
                "CurrCode": "MYR",
                "Business": "",
                "Term": "30 Days"
            },
            "2": {
                "CustomerCode": "CUST00002",
                "Name": "Intel Technology Sdn Bhd",
                "Add1": "Bayan Lepas Free Industrial Zone,Phase 3,Halaman Kampung Jawa; Bayan Lepas; Pulau Pinang; 11900",
                "Add2": "",
                "Add3": "",
                "Daddr1": "1st Flr,Standard Chartered Bank Chambers,Lebuh Pantai;Penang; Kuala Lumpur; Wilayah Persekutuan; 10300",
                "Daddr2": "",
                "Daddr3": "",
                "Attn": "",
                "Phone": "",
                "PhoneA": "",
                "Fax": "",
                "Area": "",
                "Agent": "",
                "Email": "",
                "CurrCode": "MYR",
                "Business": "",
                "Term": "30 Days"
            },
            "3": {
                "CustomerCode": "CUST00003",
                "Name": "Petronas Carigali Sdn. Bhd.",
                "Add1": "Tower 1, Petronas Twin Towers,Kuala Lumpur City Centre,; Kuala Lumpur; Wilayah Persekutuan; 50088",
                "Add2": "",
                "Add3": "",
                "Daddr1": "Tower 1,Petronas Twin Towers,K.L.City Centre; Kuala Lumpur; Wilayah Persekutuan; 50088",
                "Daddr2": "",
                "Daddr3": "",
                "Attn": "",
                "Phone": "",
                "PhoneA": "",
                "Fax": "",
                "Area": "",
                "Agent": "",
                "Email": "",
                "CurrCode": "MYR",
                "Business": "",
                "Term": "30 Days"
            }
        }
    }
}

我有以下类结构

public class CustomerInfo
        {
            public string CustomerCode { get; set; }
            public string Name { get; set; }
            public string Add1 { get; set; }
            public string Add2 { get; set; }
            public string Add3 { get; set; }
            public string Daddr1 { get; set; }
            public string Daddr2 { get; set; }
            public string Daddr3 { get; set; }
            public string Attn { get; set; }
            public string Phone { get; set; }
            public string PhoneA { get; set; }
            public string Fax { get; set; }
            public string Area { get; set; }
            public string Agent { get; set; }
            public string Email { get; set; }
            public string CurrCode { get; set; }
            public string Business { get; set; }
            public string Term { get; set; }
        }

        public class CustomerData
        {
            public CustomerInfo customerInfo { get; set; }
        }

        public class Data
        {
            public string mode { get; set; }
            public CustomerData custdata { get; set; }
        }

        public class CustomerRootObject
        {
            public bool success { get; set; }
            public string message { get; set; }
            public Data data { get; set; }
        }
    }

我很难解析我在下面编写的这种 json 格式,但它没有任何帮助,非常感谢。

List<CustomerRootObject> customerlist = JsonConvert.DeserializeObject<List<CustomerRootObject>> (json);

foreach (var info in customerlist) {Console.WriteLine(info.data.custdata.CustomerCode);}
4

2 回答 2

4

它不起作用,因为您的类结构与 JSON 不匹配。

尝试将您的Data课程更改为:

public class Data
{
    public string mode { get; set; }
    [JsonProperty("data")]
    public Dictionary<int, CustomerInfo> customers { get; set; }
}

然后,像这样反序列化:

CustomerRootObject obj = JsonConvert.DeserializeObject<CustomerRootObject>(json);

您可以这样列出客户:

foreach (KeyValuePair<int, CustomerInfo> kvp in obj.data.customers)
{
    Console.WriteLine("----- Customer " + kvp.Key + " -----");
    Console.WriteLine("CustomerCode: " + kvp.Value.CustomerCode);
    Console.WriteLine("Name: " + kvp.Value.Name);
    Console.WriteLine("Add1: " + kvp.Value.Add1.Replace(";", "\r\n"));
}

完整演示:

class Program
{
    static void Main(string[] args)
    {
        string json = @"
        {
            ""success"": true,
            ""message"": ""Missing ajax operation. Please contact administrator."",
            ""data"": {
                ""mode"": ""new"",
                ""data"": {
                    ""1"": {
                        ""CustomerCode"": ""CUST00001"",
                        ""Name"": ""Dell Asia Pacific Sdn. Bhd."",
                        ""Add1"": ""Plot 76 Mukim 11, Bukit Tengah Industrial Park; Bukit Mertajam; Pulau Pinang; 14000"",
                        ""Add2"": """",
                        ""Add3"": """",
                        ""Daddr1"": ""Level 21, Suite 21.01, The Gardens South Tower, Mid Valley City, Lingkaran Syed Putra, ; Kuala Lumpur; Wilayah Persekutuan; 59200"",
                        ""Daddr2"": """",
                        ""Daddr3"": """",
                        ""Attn"": """",
                        ""Phone"": """",
                        ""PhoneA"": """",
                        ""Fax"": """",
                        ""Area"": """",
                        ""Agent"": """",
                        ""Email"": """",
                        ""CurrCode"": ""MYR"",
                        ""Business"": """",
                        ""Term"": ""30 Days""
                    },
                    ""2"": {
                        ""CustomerCode"": ""CUST00002"",
                        ""Name"": ""Intel Technology Sdn Bhd"",
                        ""Add1"": ""Bayan Lepas Free Industrial Zone,Phase 3,Halaman Kampung Jawa; Bayan Lepas; Pulau Pinang; 11900"",
                        ""Add2"": """",
                        ""Add3"": """",
                        ""Daddr1"": ""1st Flr,Standard Chartered Bank Chambers,Lebuh Pantai;Penang; Kuala Lumpur; Wilayah Persekutuan; 10300"",
                        ""Daddr2"": """",
                        ""Daddr3"": """",
                        ""Attn"": """",
                        ""Phone"": """",
                        ""PhoneA"": """",
                        ""Fax"": """",
                        ""Area"": """",
                        ""Agent"": """",
                        ""Email"": """",
                        ""CurrCode"": ""MYR"",
                        ""Business"": """",
                        ""Term"": ""30 Days""
                    },
                    ""3"": {
                        ""CustomerCode"": ""CUST00003"",
                        ""Name"": ""Petronas Carigali Sdn. Bhd."",
                        ""Add1"": ""Tower 1, Petronas Twin Towers,Kuala Lumpur City Centre,; Kuala Lumpur; Wilayah Persekutuan; 50088"",
                        ""Add2"": """",
                        ""Add3"": """",
                        ""Daddr1"": ""Tower 1,Petronas Twin Towers,K.L.City Centre; Kuala Lumpur; Wilayah Persekutuan; 50088"",
                        ""Daddr2"": """",
                        ""Daddr3"": """",
                        ""Attn"": """",
                        ""Phone"": """",
                        ""PhoneA"": """",
                        ""Fax"": """",
                        ""Area"": """",
                        ""Agent"": """",
                        ""Email"": """",
                        ""CurrCode"": ""MYR"",
                        ""Business"": """",
                        ""Term"": ""30 Days""
                    }
                }
            }
        }";

        CustomerRootObject obj = JsonConvert.DeserializeObject<CustomerRootObject>(json);

        foreach (KeyValuePair<int, CustomerInfo> kvp in obj.data.customers)
        {
            Console.WriteLine("----- Customer " + kvp.Key + " -----");
            Console.WriteLine("CustomerCode: " + kvp.Value.CustomerCode);
            Console.WriteLine("Name: " + kvp.Value.Name);
            Console.WriteLine("Add1: " + kvp.Value.Add1.Replace(";", "\r\n"));
        }
        Console.WriteLine("\nPress a key to Exit...");
        Console.ReadKey();

    }

    public class CustomerInfo
    {
        public string CustomerCode { get; set; }
        public string Name { get; set; }
        public string Add1 { get; set; }
        public string Add2 { get; set; }
        public string Add3 { get; set; }
        public string Daddr1 { get; set; }
        public string Daddr2 { get; set; }
        public string Daddr3 { get; set; }
        public string Attn { get; set; }
        public string Phone { get; set; }
        public string PhoneA { get; set; }
        public string Fax { get; set; }
        public string Area { get; set; }
        public string Agent { get; set; }
        public string Email { get; set; }
        public string CurrCode { get; set; }
        public string Business { get; set; }
        public string Term { get; set; }
    }

    public class Data
    {
        public string mode { get; set; }
        [JsonProperty("data")]
        public Dictionary<int, CustomerInfo> customers { get; set; }
    }

    public class CustomerRootObject
    {
        public bool success { get; set; }
        public string message { get; set; }
        public Data data { get; set; }
    }
}

输出:

----- Customer 1 -----
CustomerCode: CUST00001
Name: Dell Asia Pacific Sdn. Bhd.
Add1: Plot 76 Mukim 11, Bukit Tengah Industrial Park
 Bukit Mertajam
 Pulau Pinang
 14000
----- Customer 2 -----
CustomerCode: CUST00002
Name: Intel Technology Sdn Bhd
Add1: Bayan Lepas Free Industrial Zone,Phase 3,Halaman Kampung Jawa
 Bayan Lepas
 Pulau Pinang
 11900
----- Customer 3 -----
CustomerCode: CUST00003
Name: Petronas Carigali Sdn. Bhd.
Add1: Tower 1, Petronas Twin Towers,Kuala Lumpur City Centre,
 Kuala Lumpur
 Wilayah Persekutuan
 50088
于 2013-11-06T17:24:13.520 回答
0

看起来您的 JSON 格式不正确(或者您的类没有正确的结构)。你能改变哪一个?

首先,就像@Tobberoth 所说,您不应该反序列化为列表。

    CustomerRootObject customer = JsonConvert.DeserializeObject<CustomerRootObject> (json)

您的数据“列表”不在 CustomerRootObject 级别 - 它在 CustomerInfo 级别。

    public class CustomerData
    {
        public List<CustomerInfo> customerInfo { get; set; }
    }

其次,您需要更改此类:

    public class Data
    {
        public CustomerData data { get; set; }
    }

您的 JSON 属性称为数据。您需要更改其中一个。

于 2013-11-06T17:14:20.927 回答