我正在使用API响应,并且该响应包含项目列表。这是示例JSON
{
"attendeeid": "1",
"responses": {
"1": {
"questionid": "1",
"fieldname": "1",
"name": "question?",
"pageid": "2",
"page": "Attendee Information",
"auto_capitalize": "0",
"choicekey": "",
"response": [
" Identify need",
" Evaluate products and services"
]
},
"2": {
"questionid": "2",
"fieldname": "2",
"name": "question2",
"pageid": "2",
"page": "Attendee Information",
"auto_capitalize": "0",
"choicekey": "live",
"response": "live"
},
},
}
如您所见,响应中的响应字段可以是对象或字符串。
如何创建可以反序列化的POCO对象?目前我的课程是
public class RegistrantInfoResponse
{
public string questionid { get; set; }
public string fieldname { get; set; }
public string name { get; set; }
public string response { get; set; }
}
public class RegistrantInfo
{
public string attendeeid { get; set; }
public List<RegistrantInfoResponse> responses { get; set; }
}
像这样更改您的Json:
{
"attendeeid": "1",
"responses": {
"QuestionOne": {
"questionid": "1",
"fieldname": "1",
"name": "question?",
"pageid": "2",
"page": "Attendee Information",
"auto_capitalize": "0",
"choicekey": "",
"response": [
" Identify need",
" Evaluate products and services"
]
},
"QuestionTwo": {
"questionid": "2",
"fieldname": "2",
"name": "question2",
"pageid": "2",
"page": "Attendee Information",
"auto_capitalize": "0",
"choicekey": "live",
"response": "live"
}
}
}
在json2csharp上产生以下类集:
public class QuestionOne
{
public string questionid { get; set; }
public string fieldname { get; set; }
public string name { get; set; }
public string pageid { get; set; }
public string page { get; set; }
public string auto_capitalize { get; set; }
public string choicekey { get; set; }
public List<string> response { get; set; }
}
public class QuestionTwo
{
public string questionid { get; set; }
public string fieldname { get; set; }
public string name { get; set; }
public string pageid { get; set; }
public string page { get; set; }
public string auto_capitalize { get; set; }
public string choicekey { get; set; }
public string response { get; set; }
}
public class Responses
{
public QuestionOne QuestionOne { get; set; }
public QuestionTwo QuestionTwo { get; set; }
}
public class RootObject
{
public string attendeeid { get; set; }
public Responses responses { get; set; }
}
哪个应该让您先于序列化/反序列化。