JSON序列化问题

时间:2016-10-04 07:02:19

标签: c# json

我在尝试将json字符串反序列化为对象时遇到了一些问题,我无法弄清楚问题。

https://dotnetfiddle.net/mTiYAS

我已经创建了这个dotnetfiddle来试图找出它,但我看不出问题并且不理解错误。

有人可以指出我正确的方向。

下面的Json字符串

                [
               {
                  "data":{
                     "status":"document.completed",
                     "name":"PARQ and Waiver",
                     "recipients":[
                        {
                           "first_name":"Chris",
                           "last_name":"Smith",
                           "email":"chris@domainname.com",
                           "role":"",
                           "recipient_type":"CC",
                           "has_completed":true,
                           "id":"9TtfZ8FE2XDaZi3t7AfKnj"
                        },
                        {
                           "first_name":"Chris",
                           "last_name":"Smith",
                           "email":"chris+uktest@domainname.com",
                           "role":"Member",
                           "recipient_type":"signer",
                           "has_completed":true,
                           "id":"NQXY5GZkGXozUu4apNEV2Z"
                        }
                     ],
                     "date_modified":"2016-10-04T05:50:07.484635Z",
                     "fields":[
                        {
                           "assignee":"chris+uktest@domainname.com",
                           "title":"",
                           "uuid":"Kzm9GTTomzao3fmcC5U968",
                           "value":"dasdasd",
                           "name":"Textfield"
                        },
                        {
                           "assignee":"chris+uktest@domainname.com",
                           "title":"",
                           "uuid":"3KUNJTsGnSA3PUiPTnKqVo",
                           "value":"3asdas",
                           "name":"EmergencyContactName"
                        },
                        {
                           "assignee":"chris+uktest@domainname.com",
                           "title":"",
                           "uuid":"TcDrKky4ahtU5BvgYgqiCZ",
                           "value":"12312",
                           "name":"MemberICNumber"
                        },
                        {
                           "assignee":"chris+uktest@domainname.com",
                           "title":"MemberName",
                           "uuid":"XjNFVWoG8gPhFGEVnHXCrE",
                           "value":"Chris Smith",
                           "name":"MemberName"
                        },
                        {
                           "assignee":"chris+uktest@domainname.com",
                           "title":"Signature",
                           "uuid":"ZWRPXG6UUP86A8FvCRmpcZ",
                           "value":{

                           },
                           "name":"Signature"
                        },
                        {
                           "assignee":"chris+uktest@domainname.com",
                           "title":"Signature",
                           "uuid":"bLercwpbgqKFcLG3TTqsRD",
                           "value":{

                           },
                           "name":"Signature"
                        },
                        {
                           "assignee":"chris+uktest@domainname.com",
                           "title":"CurrentDate",
                           "uuid":"rFpaSpQXEsuu4KqcUS9MJT",
                           "value":"2016-10-04T00:00:00.000Z",
                           "name":"CurrentDate"
                        },
                        {
                           "assignee":"chris+uktest@domainname.com",
                           "title":"EmployeeName",
                           "uuid":"GoqRq7WoRad3umjcgtCA6H",
                           "value":"Christopher Smith",
                           "name":"EmployeeName"
                        },
                        {
                           "assignee":"chris+uktest@domainname.com",
                           "title":"Checkbox",
                           "uuid":"krEjqFZXNjYXAhT3d4eabU",
                           "value":{

                           },
                           "name":"Checkbox"
                        },
                        {
                           "assignee":"chris+uktest@domainname.com",
                           "title":"Checkbox",
                           "uuid":"Aa3AeywRfrkco7hyodL83V",
                           "value":true,
                           "name":"Checkbox"
                        },
                        {
                           "assignee":"chris+uktest@domainname.com",
                           "title":"Checkbox",
                           "uuid":"o83dM7bE956W7b2Pek9LuR",
                           "value":{

                           },
                           "name":"Checkbox"
                        },
                        {
                           "assignee":"chris+uktest@domainname.com",
                           "title":"Checkbox",
                           "uuid":"DrkJrj8LgZJLDTQvmeLt6S",
                           "value":{

                           },
                           "name":"Checkbox"
                        },
                        {
                           "assignee":"chris+uktest@domainname.com",
                           "title":"Checkbox",
                           "uuid":"QLvkZPJzcK4QmCunbsuJMm",
                           "value":true,
                           "name":"Checkbox"
                        },
                        {
                           "assignee":"chris+uktest@domainname.com",
                           "title":"Checkbox",
                           "uuid":"UusciRwckWig6fAzGPqvdM",
                           "value":true,
                           "name":"Checkbox"
                        },
                        {
                           "assignee":"chris+uktest@domainname.com",
                           "title":"Checkbox",
                           "uuid":"SfwCUPfsWP4ufMdbPUsoYN",
                           "value":{

                           },
                           "name":"Checkbox"
                        },
                        {
                           "assignee":"chris+uktest@domainname.com",
                           "title":"Checkbox",
                           "uuid":"F4vn8B39cZcdzmoycaMk7d",
                           "value":{

                           },
                           "name":"Checkbox"
                        },
                        {
                           "assignee":"chris+uktest@domainname.com",
                           "title":"Checkbox",
                           "uuid":"BxcC5Gme3v6d6xAM2o6Euh",
                           "value":true,
                           "name":"Checkbox"
                        },
                        {
                           "assignee":"chris+uktest@domainname.com",
                           "title":"Checkbox",
                           "uuid":"fRCKZMkUQuT5roh9mG98JG",
                           "value":{

                           },
                           "name":"Checkbox"
                        },
                        {
                           "assignee":"chris+uktest@domainname.com",
                           "title":"Checkbox",
                           "uuid":"UgxwN3BmJvZm7NZFvnMExi",
                           "value":true,
                           "name":"Checkbox"
                        },
                        {
                           "assignee":"chris+uktest@domainname.com",
                           "title":"Checkbox",
                           "uuid":"Ah8qEhU8wvkFA7nFJmVzMA",
                           "value":{

                           },
                           "name":"Checkbox"
                        },
                        {
                           "assignee":"chris+uktest@domainname.com",
                           "title":"Checkbox",
                           "uuid":"KrmFKD5vM8fzeGuDHmAqLe",
                           "value":true,
                           "name":"Checkbox"
                        },
                        {
                           "assignee":"chris+uktest@domainname.com",
                           "title":"Checkbox",
                           "uuid":"x5VJRphNSZRgS73bZxSBCm",
                           "value":true,
                           "name":"Checkbox"
                        }
                     ],
                     "tags":[

                     ],
                     "sent_by":{
                        "email":"chris@domainname.com",
                        "first_name":"Chris",
                        "last_name":"Smith",
                        "id":"QWYXpQw4zyd2y3U2py8cc9",
                        "avatar":null
                     },
                     "created_by":{
                        "email":"chris@domainname.com",
                        "first_name":"Chris",
                        "last_name":"Smith",
                        "id":"QWYXpQw4zyd2y3U2py8cc9",
                        "avatar":null
                     },
                     "tokens":[

                     ],
                     "action_date":"2016-10-04T05:50:07.783684",
                     "action_by":{
                        "first_name":"Chris",
                        "last_name":"Smith",
                        "id":"JAct5Xj5N6WUbCf8KRpFFc",
                        "email":"chris+uktest@domainname.com"
                     },
                     "date_created":"2016-10-04T05:50:07.484600Z",
                     "id":"uFLyzUS68hkyTu2QFgLmtN",
                     "metadata":{

                     }
                  },
                  "event":"recipient_completed"
               }
            ]

用于反序列化字符串的代码是:

using System;
using Newtonsoft.Json;      
using Newtonsoft.Json.Linq;                 
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        string json = "[{\"data\": {\"status\": \"document.completed\", \"name\": \"PARQ and Waiver (Final 15 September 2016)\", \"recipients\": [{\"first_name\": \"Chris\", \"last_name\": \"Pettigrew\", \"email\": \"chris@ritualgym.com\", \"role\": \"\", \"recipient_type\": \"CC\", \"has_completed\": true, \"id\": \"nsCaT2VWLnctgKge77ZfQL\"}, {\"first_name\": \"Chris\", \"last_name\": \"Pettigrew\", \"email\": \"chris+uktest@ritualgym.com\", \"role\": \"Member\", \"recipient_type\": \"signer\", \"has_completed\": true, \"id\": \"AHXfWQuH8MkbL9vxzfhtsc\"}], \"date_modified\": \"2016-10-04T07:43:14.353443Z\", \"fields\": [{\"assignee\": \"chris+uktest@ritualgym.com\", \"title\": \"\", \"uuid\": \"pB5cNLi7nm3LMNBChbkURT\", \"value\": \"asdasd\", \"name\": \"Textfield\"}, {\"assignee\": \"chris+uktest@ritualgym.com\", \"title\": \"\", \"uuid\": \"HawALj7sghbpwKQG4Ft949\", \"value\": \"asdasd\", \"name\": \"EmergencyContactName\"}, {\"assignee\": \"chris+uktest@ritualgym.com\", \"title\": \"\", \"uuid\": \"hmMLPtCfEYGDE8QwehCbVj\", \"value\": \"12313\", \"name\": \"MemberICNumber\"}, {\"assignee\": \"chris+uktest@ritualgym.com\", \"title\": \"MemberName\", \"uuid\": \"V66jyajqeBDmPF9mzFJA86\", \"value\": \"Chris Pettigrew\", \"name\": \"MemberName\"}, {\"assignee\": \"chris+uktest@ritualgym.com\", \"title\": \"Signature\", \"uuid\": \"GhGkMnm6GnfaZ5yeV4GtVR\", \"value\": {}, \"name\": \"Signature\"}, {\"assignee\": \"chris+uktest@ritualgym.com\", \"title\": \"Signature\", \"uuid\": \"hjGimFc83PwZoeMXdFEVQc\", \"value\": {}, \"name\": \"Signature\"}, {\"assignee\": \"chris+uktest@ritualgym.com\", \"title\": \"CurrentDate\", \"uuid\": \"jRqpik4oia3nMN8Fp4Bk5S\", \"value\": \"2016-10-04T00:00:00.000Z\", \"name\": \"CurrentDate\"}, {\"assignee\": \"chris+uktest@ritualgym.com\", \"title\": \"EmployeeName\", \"uuid\": \"4bdjuVw6BhhuPxu8cetRnV\", \"value\": \"Christopher Pettigrew\", \"name\": \"EmployeeName\"}, {\"assignee\": \"chris+uktest@ritualgym.com\", \"title\": \"Checkbox\", \"uuid\": \"tvx2UBJWQvktdtuighyt67\", \"value\": {}, \"name\": \"Checkbox\"}, {\"assignee\": \"chris+uktest@ritualgym.com\", \"title\": \"Checkbox\", \"uuid\": \"oB5S2Qc3uUkJKL9JZxUbpa\", \"value\": true, \"name\": \"Checkbox\"}, {\"assignee\": \"chris+uktest@ritualgym.com\", \"title\": \"Checkbox\", \"uuid\": \"Z3nSEm3t7CYa674TSA3A9i\", \"value\": {}, \"name\": \"Checkbox\"}, {\"assignee\": \"chris+uktest@ritualgym.com\", \"title\": \"Checkbox\", \"uuid\": \"H7BiW7Tuow8qhnt3yauDKH\", \"value\": {}, \"name\": \"Checkbox\"}, {\"assignee\": \"chris+uktest@ritualgym.com\", \"title\": \"Checkbox\", \"uuid\": \"mWjfHa5NHRxdWXSaUwE3HG\", \"value\": true, \"name\": \"Checkbox\"}, {\"assignee\": \"chris+uktest@ritualgym.com\", \"title\": \"Checkbox\", \"uuid\": \"hS7xw28cCwkybVpL8Jji9F\", \"value\": true, \"name\": \"Checkbox\"}, {\"assignee\": \"chris+uktest@ritualgym.com\", \"title\": \"Checkbox\", \"uuid\": \"NX9swWbGcjUky2uXoVy8o3\", \"value\": {}, \"name\": \"Checkbox\"}, {\"assignee\": \"chris+uktest@ritualgym.com\", \"title\": \"Checkbox\", \"uuid\": \"WU3xPDzsuxnZv6sMQ7uoLg\", \"value\": {}, \"name\": \"Checkbox\"}, {\"assignee\": \"chris+uktest@ritualgym.com\", \"title\": \"Checkbox\", \"uuid\": \"UavefJS7JjvFnRc3oRMRA6\", \"value\": true, \"name\": \"Checkbox\"}, {\"assignee\": \"chris+uktest@ritualgym.com\", \"title\": \"Checkbox\", \"uuid\": \"cBzK94gzweDU7BcmDXoCn6\", \"value\": {}, \"name\": \"Checkbox\"}, {\"assignee\": \"chris+uktest@ritualgym.com\", \"title\": \"Checkbox\", \"uuid\": \"VsHp3MaHcQtroCNLPwsRPW\", \"value\": true, \"name\": \"Checkbox\"}, {\"assignee\": \"chris+uktest@ritualgym.com\", \"title\": \"Checkbox\", \"uuid\": \"qz9gChVGDim5BGT8ojbReR\", \"value\": {}, \"name\": \"Checkbox\"}, {\"assignee\": \"chris+uktest@ritualgym.com\", \"title\": \"Checkbox\", \"uuid\": \"sUM4HB5BozZxJxoUEJ5CxH\", \"value\": true, \"name\": \"Checkbox\"}, {\"assignee\": \"chris+uktest@ritualgym.com\", \"title\": \"Checkbox\", \"uuid\": \"aZuXSaeM6LoMmDswJ2oUBT\", \"value\": true, \"name\": \"Checkbox\"}], \"tags\": [], \"sent_by\": {\"email\": \"chris@ritualgym.com\", \"first_name\": \"Chris\", \"last_name\": \"Pettigrew\", \"id\": \"QWYXpQw4zyd2y3U2py8cc9\", \"avatar\": null}, \"created_by\": {\"email\": \"chris@ritualgym.com\", \"first_name\": \"Chris\", \"last_name\": \"Pettigrew\", \"id\": \"QWYXpQw4zyd2y3U2py8cc9\", \"avatar\": null}, \"tokens\": [], \"action_date\": \"2016-10-04T07:43:14.669400\", \"action_by\": {\"first_name\": \"Chris\", \"last_name\": \"Pettigrew\", \"id\": \"JAct5Xj5N6WUbCf8KRpFFc\", \"email\": \"chris+uktest@ritualgym.com\"}, \"date_created\": \"2016-10-04T07:43:14.353410Z\", \"id\": \"8QnPa8jUHrF7RNf5s2HSVD\", \"metadata\": {}}, \"event\": \"recipient_completed\"}]";

    Notification response = JsonConvert.DeserializeObject<Notification>(json);
    }
}

    public class Notification
    {
        [JsonProperty("data")]
        public Document Document { get; set; }

        [JsonProperty("event")]
        public string Event { get; set; }
    }

public class Document
    {
        [JsonProperty("status")]
        public string Status { get; set; }

        [JsonProperty("name")]
        public string Name { get; set; }

        [JsonProperty("recipients")]
        public Recipient[] Recipients { get; set; }

        [JsonProperty("date_modified")]
        public string DateModified { get; set; }

        [JsonProperty("fields")]
        public Fields[] Fields { get; set; }

        [JsonProperty("tags")]
        public string[] Tags { get; set; }

        [JsonProperty("sent_by")]
        public User SendBy { get; set; }

        [JsonProperty("created_by")]
        public User CreatedBy { get; set; }

        [JsonProperty("tokens")]
        public Token[] Tokens { get; set; }

        [JsonProperty("action_date")]
        public string ActionDate { get; set; }

        [JsonProperty("action_by")]
        public User ActionBy { get; set; }

        [JsonProperty("date_created")]
        public string DateCreated { get; set; }

        [JsonProperty("id")]
        public string Id { get; set; }

        [JsonProperty("metadata")]
        public Dictionary<string, string> Metadata { get; set; }
    }
    public class Fields
    {
        [JsonProperty("assignee")]
        public string Assignee { get; set; }

        [JsonProperty("title")]
        public string Title { get; set; }

        [JsonProperty("uuid")]
        public string UUID { get; set; }

        [JsonProperty("value")]
        public string Value{ get; set; }

        [JsonProperty("name")]
        public string Name { get; set; }
    }

    public class Recipient
    {
        [JsonProperty("first_name")]
        public string FirstName { get; set; }

        [JsonProperty("last_name")]
        public string LastName { get; set; }

        [JsonProperty("role")]
        public string Role { get; set; }

        [JsonProperty("email")]
        public string Email { get; set; }

        [JsonProperty("recipient_type")]
        public string RecipientType { get; set; }

        [JsonProperty("has_completed")]
        public bool HasCompleted { get; set; }

        [JsonProperty("id")]
        public string Id { get; set; }
    }

    public class Token
    {
        [JsonProperty("name")]
        public string Name { get; set; }
        [JsonProperty("value")]
        public string Value{ get; set; }
    }

    public class User
    {
        [JsonProperty("email")]
        public string Email { get; set; }
        [JsonProperty("first_name")]
        public string FirstName { get; set; }

        [JsonProperty("last_name")]
        public string LastName { get; set; }

        [JsonProperty("id")]
        public string Id { get; set; }

        [JsonProperty("avatar")]
        public string Avatar { get; set; }
    }

1 个答案:

答案 0 :(得分:0)

请检查此模型类以反序列化json对象。

public class Recipient
{
    public string first_name { get; set; }
    public string last_name { get; set; }
    public string email { get; set; }
    public string role { get; set; }
    public string recipient_type { get; set; }
    public bool has_completed { get; set; }
    public string id { get; set; }
}

public class Field
{
    public string assignee { get; set; }
    public string title { get; set; }
    public string uuid { get; set; }
    public object value { get; set; }
    public string name { get; set; }
}

public class SentBy
{
    public string email { get; set; }
    public string first_name { get; set; }
    public string last_name { get; set; }
    public string id { get; set; }
    public object avatar { get; set; }
}

public class CreatedBy
{
    public string email { get; set; }
    public string first_name { get; set; }
    public string last_name { get; set; }
    public string id { get; set; }
    public object avatar { get; set; }
}

public class ActionBy
{
    public string first_name { get; set; }
    public string last_name { get; set; }
    public string id { get; set; }
    public string email { get; set; }
}

public class Metadata
{
}

public class Data
{
    public string status { get; set; }
    public string name { get; set; }
    public List<Recipient> recipients { get; set; }
    public string date_modified { get; set; }
    public List<Field> fields { get; set; }
    public List<object> tags { get; set; }
    public SentBy sent_by { get; set; }
    public CreatedBy created_by { get; set; }
    public List<object> tokens { get; set; }
    public string action_date { get; set; }
    public ActionBy action_by { get; set; }
    public string date_created { get; set; }
    public string id { get; set; }
    public Metadata metadata { get; set; }
}

public class RootObject
{
    public Data data { get; set; }
    public string @event { get; set; }
}

或者尝试查看此网站http://json2csharp.com/