反序列化复杂的JSON

时间:2017-12-06 08:15:15

标签: c# json

我正在尝试反序列化这个JSON:

{
  "request": {
    "Target": "Affiliate",
    "Format": "json",
    "Service": "HasOffers",
    "Version": "2",
    "NetworkToken": "NETVTxpCN4QxoWuE6D6SCdh67itLvG",
    "Method": "getSignupAnswers",
    "id": "9820",
    "__lc_visitor_id_1040387": "S1501672650.ef9130f70a",
    "optimizelyEndUserId": "oeu1504526823197r0.9821082007049919",
    "optimizelySegments": "{\"3005961667\":\"campaign\",\"3007971800\":\"gc\",\"3016921538\":\"false\"}",
    "optimizelyBuckets": "{\"8910731217\":\"8900223657\"}",
    "_mkto_trk": "id:210-BFY-977&token:_mch-hasoffers.com-1504526823819-68924",
    "__gaTune": "GA1.2.1953499689.1501744456",
    "_hp2_id_1318563364": "{\"userId\":null,\"pageviewId\":\"5080006815314464\",\"sessionId\":\"3328773167767995\",\"identity\":\"23255-2\",\"trackerVersion\":\"3.0\"}",
    "intercom-session-zrjutp6m": "QXAxQTdza0JnNmVPLzNxZDFXSmFjbUQxRnVxQjArcmNmQWxMZzM4NWpSbWt3VXNzemFydG5oVjZYempaUlg3bS0tR0VrYWcwTGpSY3I4cDdsMFB3TndlUT09--5c9d563a96f41049f2703a3121484e9a299b4148",
    "intercom-lou-zrjutp6m": "1"
  },
  "response": {
    "status": 1,
    "httpStatus": 200,
    "data": {
      "140768": {
        "SignupAnswer": {
          "id": "140768",
          "question_id": "1",
          "responder_type": "affiliate",
          "answer": "Individual \r\n",
          "responder_user_id": null,
          "ref_id": null,
          "responder_id": "9820",
          "modified": "2017-07-04 02:55:23",
          "question": "Affiliate Type",
          "type": "affiliate",
          "status": "active"
        }
      },
      "140770": {
        "SignupAnswer": {
          "id": "140770",
          "question_id": "3",
          "responder_type": "affiliate",
          "answer": "Rugved Agro",
          "responder_user_id": null,
          "ref_id": null,
          "responder_id": "9820",
          "modified": "2017-07-04 02:55:23",
          "question": "Organisation Name",
          "type": "affiliate",
          "status": "active"
        }
      },
      "140772": {
        "SignupAnswer": {
          "id": "140772",
          "question_id": "5",
          "responder_type": "affiliate",
          "answer": "9822288117",
          "responder_user_id": null,
          "ref_id": null,
          "responder_id": "9820",
          "modified": "2017-07-04 02:55:23",
          "question": "Contact Number ",
          "type": "affiliate",
          "status": "active"
        }
      },
      "140774": {
        "SignupAnswer": {
          "id": "140774",
          "question_id": "7",
          "responder_type": "affiliate",
          "answer": "7972274145",
          "responder_user_id": null,
          "ref_id": null,
          "responder_id": "9820",
          "modified": "2017-07-04 02:55:23",
          "question": "Secondary Contact Number",
          "type": "affiliate",
          "status": "active"
        }
      },
      "140776": {
        "SignupAnswer": {
          "id": "140776",
          "question_id": "9",
          "responder_type": "affiliate",
          "answer": "",
          "responder_user_id": null,
          "ref_id": null,
          "responder_id": "9820",
          "modified": "2017-07-04 02:55:23",
          "question": "Secondary E-mail Adress",
          "type": "affiliate",
          "status": "active"
        }
      },
      "140778": {
        "SignupAnswer": {
          "id": "140778",
          "question_id": "11",
          "responder_type": "affiliate",
          "answer": "SACHIN RAMLAL AGALE",
          "responder_user_id": null,
          "ref_id": null,
          "responder_id": "9820",
          "modified": "2017-07-04 02:55:23",
          "question": "Payee Name",
          "type": "affiliate",
          "status": "active"
        }
      },
      "140780": {
        "SignupAnswer": {
          "id": "140780",
          "question_id": "13",
          "responder_type": "affiliate",
          "answer": "Electronic Fund Transfer \r\n",
          "responder_user_id": null,
          "ref_id": null,
          "responder_id": "9820",
          "modified": "2017-07-04 02:55:23",
          "question": "Payment Mode",
          "type": "affiliate",
          "status": "active"
        }
      },
      "140782": {
        "SignupAnswer": {
          "id": "140782",
          "question_id": "15",
          "responder_type": "affiliate",
          "answer": "0480104000105996",
          "responder_user_id": null,
          "ref_id": null,
          "responder_id": "9820",
          "modified": "2017-07-04 02:55:23",
          "question": "Bank Account Number",
          "type": "affiliate",
          "status": "active"
        }
      },
      "140784": {
        "SignupAnswer": {
          "id": "140784",
          "question_id": "17",
          "responder_type": "affiliate",
          "answer": "IDBI BANK ",
          "responder_user_id": null,
          "ref_id": null,
          "responder_id": "9820",
          "modified": "2017-07-04 02:55:23",
          "question": "Bank Name",
          "type": "affiliate",
          "status": "active"
        }
      },
      "140786": {
        "SignupAnswer": {
          "id": "140786",
          "question_id": "19",
          "responder_type": "affiliate",
          "answer": "IBKL0000480",
          "responder_user_id": null,
          "ref_id": null,
          "responder_id": "9820",
          "modified": "2017-07-04 02:55:23",
          "question": "IFSC Code",
          "type": "affiliate",
          "status": "active"
        }
      },
      "140788": {
        "SignupAnswer": {
          "id": "140788",
          "question_id": "21",
          "responder_type": "affiliate",
          "answer": "431259006",
          "responder_user_id": null,
          "ref_id": null,
          "responder_id": "9820",
          "modified": "2017-07-04 02:55:23",
          "question": "MICR Code",
          "type": "affiliate",
          "status": "active"
        }
      },
      "140790": {
        "SignupAnswer": {
          "id": "140790",
          "question_id": "23",
          "responder_type": "affiliate",
          "answer": "Aurangabad ",
          "responder_user_id": null,
          "ref_id": null,
          "responder_id": "9820",
          "modified": "2017-07-04 02:55:23",
          "question": "Bank City",
          "type": "affiliate",
          "status": "active"
        }
      },
      "140792": {
        "SignupAnswer": {
          "id": "140792",
          "question_id": "25",
          "responder_type": "affiliate",
          "answer": "",
          "responder_user_id": null,
          "ref_id": null,
          "responder_id": "9820",
          "modified": "2017-07-04 02:55:23",
          "question": "UPLOAD DOCUMENTS\u00a0(If required For Receiving Payments)",
          "type": "affiliate",
          "status": "deleted"
        }
      },
      "140794": {
        "SignupAnswer": {
          "id": "140794",
          "question_id": "27",
          "responder_type": "affiliate",
          "answer": "rugvedagro.blogspot.in",
          "responder_user_id": null,
          "ref_id": null,
          "responder_id": "9820",
          "modified": "2017-07-04 02:55:24",
          "question": "Website URL",
          "type": "affiliate",
          "status": "active"
        }
      },
      "140796": {
        "SignupAnswer": {
          "id": "140796",
          "question_id": "29",
          "responder_type": "affiliate",
          "answer": "Blog\r\n",
          "responder_user_id": null,
          "ref_id": null,
          "responder_id": "9820",
          "modified": "2017-07-04 02:55:24",
          "question": "Website Type",
          "type": "affiliate",
          "status": "active"
        }
      },
      "140798": {
        "SignupAnswer": {
          "id": "140798",
          "question_id": "31",
          "responder_type": "affiliate",
          "answer": "Btw 1000-10000\r\n",
          "responder_user_id": null,
          "ref_id": null,
          "responder_id": "9820",
          "modified": "2017-07-04 02:55:24",
          "question": "Monthly Visits",
          "type": "affiliate",
          "status": "active"
        }
      }
    },
    "errors": [

    ],
    "errorMessage": null
  }
}

我所拥有的课程如下:

public class Request
{
    public string Target { get; set; }
    public string Format { get; set; }
    public string Service { get; set; }
    public string Version { get; set; }
    public string NetworkToken { get; set; }
    public string Method { get; set; }
    public string id { get; set; }
    public string __lc_visitor_id_1040387 { get; set; }
    public string optimizelyEndUserId { get; set; }
    public string optimizelySegments { get; set; }
    public string optimizelyBuckets { get; set; }
    public string _mkto_trk { get; set; }
    public string __gaTune { get; set; }
    public string _hp2_id_1318563364 { get; set; }
    [JsonProperty(PropertyName = "intercom-session-zrjutp6m")]
    public string __invalid_name__intercom_session_zrjutp6m { get; set; }
    [JsonProperty(PropertyName = "intercom-lou-zrjutp6m")]
    public string __invalid_name__intercom_lou_zrjutp6m { get; set; }
}
public class SignupAnswer
{
    public string id { get; set; }
    public string question_id { get; set; }
    public string responder_type { get; set; }
    public string answer { get; set; }
    public object responder_user_id { get; set; }
    public object ref_id { get; set; }
    public string responder_id { get; set; }
    public string modified { get; set; }
    public string question { get; set; }
    public string type { get; set; }
    public string status { get; set; }
}

public class Data
{
    /* I can't code this class */

}

public class Response
{
    public int status { get; set; }
    public int httpStatus { get; set; }
    public Data data { get; set; } //I can't code this class
    public List<object> errors { get; set; }
    public object errorMessage { get; set; }
}

public class RootObject
{
    public Request request { get; set; }
    public Response response { get; set; }
}

我已经尝试过json2csharp了。这里的问题是节点响应下的数据 - &gt;数据是动态的。所以我不能有固定的班级名称

2 个答案:

答案 0 :(得分:1)

Response Class的Data属性应该是Dictionary:

public class Response
{
    public int status { get; set; }
    public int httpStatus { get; set; }
    public Dictionary<string,Data> data { get; set; }
    public object[] errors { get; set; }
    public object errorMessage { get; set; }
}

可能你需要另一个像这样的signupanswer包装器:

public class Data 
{
    public Signupanswer Signupanswer { get;set; }
}

答案 1 :(得分:0)

我在Visual Studio中使用special paste选项:

enter image description here

我得到了这个结果:

public class Rootobject
{
    public Request request { get; set; }
    public Response response { get; set; }
}

public class Request
{
    public string Target { get; set; }
    public string Format { get; set; }
    public string Service { get; set; }
    public string Version { get; set; }
    public string NetworkToken { get; set; }
    public string Method { get; set; }
    public string id { get; set; }
    public string __lc_visitor_id_1040387 { get; set; }
    public string optimizelyEndUserId { get; set; }
    public string optimizelySegments { get; set; }
    public string optimizelyBuckets { get; set; }
    public string _mkto_trk { get; set; }
    public string __gaTune { get; set; }
    public string _hp2_id_1318563364 { get; set; }
    public string intercomsessionzrjutp6m { get; set; }
    public string intercomlouzrjutp6m { get; set; }
}

public class Response
{
    public int status { get; set; }
    public int httpStatus { get; set; }
    public Data data { get; set; }
    public object[] errors { get; set; }
    public object errorMessage { get; set; }
}

public class Data
{
    public _140768 _140768 { get; set; }
    public _140770 _140770 { get; set; }
    public _140772 _140772 { get; set; }
    public _140774 _140774 { get; set; }
    public _140776 _140776 { get; set; }
    public _140778 _140778 { get; set; }
    public _140780 _140780 { get; set; }
    public _140782 _140782 { get; set; }
    public _140784 _140784 { get; set; }
    public _140786 _140786 { get; set; }
    public _140788 _140788 { get; set; }
    public _140790 _140790 { get; set; }
    public _140792 _140792 { get; set; }
    public _140794 _140794 { get; set; }
    public _140796 _140796 { get; set; }
    public _140798 _140798 { get; set; }
}

public class _140768
{
    public Signupanswer SignupAnswer { get; set; }
}

public class Signupanswer
{
    public string id { get; set; }
    public string question_id { get; set; }
    public string responder_type { get; set; }
    public string answer { get; set; }
    public object responder_user_id { get; set; }
    public object ref_id { get; set; }
    public string responder_id { get; set; }
    public string modified { get; set; }
    public string question { get; set; }
    public string type { get; set; }
    public string status { get; set; }
}

public class _140770
{
    public Signupanswer1 SignupAnswer { get; set; }
}

public class Signupanswer1
{
    public string id { get; set; }
    public string question_id { get; set; }
    public string responder_type { get; set; }
    public string answer { get; set; }
    public object responder_user_id { get; set; }
    public object ref_id { get; set; }
    public string responder_id { get; set; }
    public string modified { get; set; }
    public string question { get; set; }
    public string type { get; set; }
    public string status { get; set; }
}

public class _140772
{
    public Signupanswer2 SignupAnswer { get; set; }
}

public class Signupanswer2
{
    public string id { get; set; }
    public string question_id { get; set; }
    public string responder_type { get; set; }
    public string answer { get; set; }
    public object responder_user_id { get; set; }
    public object ref_id { get; set; }
    public string responder_id { get; set; }
    public string modified { get; set; }
    public string question { get; set; }
    public string type { get; set; }
    public string status { get; set; }
}

public class _140774
{
    public Signupanswer3 SignupAnswer { get; set; }
}

public class Signupanswer3
{
    public string id { get; set; }
    public string question_id { get; set; }
    public string responder_type { get; set; }
    public string answer { get; set; }
    public object responder_user_id { get; set; }
    public object ref_id { get; set; }
    public string responder_id { get; set; }
    public string modified { get; set; }
    public string question { get; set; }
    public string type { get; set; }
    public string status { get; set; }
}

public class _140776
{
    public Signupanswer4 SignupAnswer { get; set; }
}

public class Signupanswer4
{
    public string id { get; set; }
    public string question_id { get; set; }
    public string responder_type { get; set; }
    public string answer { get; set; }
    public object responder_user_id { get; set; }
    public object ref_id { get; set; }
    public string responder_id { get; set; }
    public string modified { get; set; }
    public string question { get; set; }
    public string type { get; set; }
    public string status { get; set; }
}

public class _140778
{
    public Signupanswer5 SignupAnswer { get; set; }
}

public class Signupanswer5
{
    public string id { get; set; }
    public string question_id { get; set; }
    public string responder_type { get; set; }
    public string answer { get; set; }
    public object responder_user_id { get; set; }
    public object ref_id { get; set; }
    public string responder_id { get; set; }
    public string modified { get; set; }
    public string question { get; set; }
    public string type { get; set; }
    public string status { get; set; }
}

public class _140780
{
    public Signupanswer6 SignupAnswer { get; set; }
}

public class Signupanswer6
{
    public string id { get; set; }
    public string question_id { get; set; }
    public string responder_type { get; set; }
    public string answer { get; set; }
    public object responder_user_id { get; set; }
    public object ref_id { get; set; }
    public string responder_id { get; set; }
    public string modified { get; set; }
    public string question { get; set; }
    public string type { get; set; }
    public string status { get; set; }
}

public class _140782
{
    public Signupanswer7 SignupAnswer { get; set; }
}

public class Signupanswer7
{
    public string id { get; set; }
    public string question_id { get; set; }
    public string responder_type { get; set; }
    public string answer { get; set; }
    public object responder_user_id { get; set; }
    public object ref_id { get; set; }
    public string responder_id { get; set; }
    public string modified { get; set; }
    public string question { get; set; }
    public string type { get; set; }
    public string status { get; set; }
}

public class _140784
{
    public Signupanswer8 SignupAnswer { get; set; }
}

public class Signupanswer8
{
    public string id { get; set; }
    public string question_id { get; set; }
    public string responder_type { get; set; }
    public string answer { get; set; }
    public object responder_user_id { get; set; }
    public object ref_id { get; set; }
    public string responder_id { get; set; }
    public string modified { get; set; }
    public string question { get; set; }
    public string type { get; set; }
    public string status { get; set; }
}

public class _140786
{
    public Signupanswer9 SignupAnswer { get; set; }
}

public class Signupanswer9
{
    public string id { get; set; }
    public string question_id { get; set; }
    public string responder_type { get; set; }
    public string answer { get; set; }
    public object responder_user_id { get; set; }
    public object ref_id { get; set; }
    public string responder_id { get; set; }
    public string modified { get; set; }
    public string question { get; set; }
    public string type { get; set; }
    public string status { get; set; }
}

public class _140788
{
    public Signupanswer10 SignupAnswer { get; set; }
}

public class Signupanswer10
{
    public string id { get; set; }
    public string question_id { get; set; }
    public string responder_type { get; set; }
    public string answer { get; set; }
    public object responder_user_id { get; set; }
    public object ref_id { get; set; }
    public string responder_id { get; set; }
    public string modified { get; set; }
    public string question { get; set; }
    public string type { get; set; }
    public string status { get; set; }
}

public class _140790
{
    public Signupanswer11 SignupAnswer { get; set; }
}

public class Signupanswer11
{
    public string id { get; set; }
    public string question_id { get; set; }
    public string responder_type { get; set; }
    public string answer { get; set; }
    public object responder_user_id { get; set; }
    public object ref_id { get; set; }
    public string responder_id { get; set; }
    public string modified { get; set; }
    public string question { get; set; }
    public string type { get; set; }
    public string status { get; set; }
}

public class _140792
{
    public Signupanswer12 SignupAnswer { get; set; }
}

public class Signupanswer12
{
    public string id { get; set; }
    public string question_id { get; set; }
    public string responder_type { get; set; }
    public string answer { get; set; }
    public object responder_user_id { get; set; }
    public object ref_id { get; set; }
    public string responder_id { get; set; }
    public string modified { get; set; }
    public string question { get; set; }
    public string type { get; set; }
    public string status { get; set; }
}

public class _140794
{
    public Signupanswer13 SignupAnswer { get; set; }
}

public class Signupanswer13
{
    public string id { get; set; }
    public string question_id { get; set; }
    public string responder_type { get; set; }
    public string answer { get; set; }
    public object responder_user_id { get; set; }
    public object ref_id { get; set; }
    public string responder_id { get; set; }
    public string modified { get; set; }
    public string question { get; set; }
    public string type { get; set; }
    public string status { get; set; }
}

public class _140796
{
    public Signupanswer14 SignupAnswer { get; set; }
}

public class Signupanswer14
{
    public string id { get; set; }
    public string question_id { get; set; }
    public string responder_type { get; set; }
    public string answer { get; set; }
    public object responder_user_id { get; set; }
    public object ref_id { get; set; }
    public string responder_id { get; set; }
    public string modified { get; set; }
    public string question { get; set; }
    public string type { get; set; }
    public string status { get; set; }
}

public class _140798
{
    public Signupanswer15 SignupAnswer { get; set; }
}

public class Signupanswer15
{
    public string id { get; set; }
    public string question_id { get; set; }
    public string responder_type { get; set; }
    public string answer { get; set; }
    public object responder_user_id { get; set; }
    public object ref_id { get; set; }
    public string responder_id { get; set; }
    public string modified { get; set; }
    public string question { get; set; }
    public string type { get; set; }
    public string status { get; set; }
}