{
"QueryResponse": {
"Customer": [
{
"GivenName": "Test"
},
{
"GivenName": "Test 2"
}
],
"startPosition": 1,
"maxResults": 2
},
"time": "2018-08-11T16:12:10.808-07:00"
}
我正试图从以下内容创建一个C#模型:
public class QueryResponseRoot<T> where T : IQuickbooksBaseEntity
{
[JsonProperty("QueryResponse")]
public QueryResponse<T> QueryResponse { get; set; }
[JsonProperty("time", Required = Required.Always)]
public DateTimeOffset Time { get; set; }
}
public class QueryResponse<T> where T : IQuickbooksBaseEntity
{
[JsonProperty, JsonConverter(typeof(QuickbooksResponseConverter))]
public IList<T> Results { get; set; }
[JsonProperty("startPosition")]
public Int64 StartPositions { get; set; }
[JsonProperty("maxResults")]
public Int64 MaxResults { get; set; }
}
IQuickbooksBaseEntity
接口。
问题是如果你
Customers
它会回来的
Customer
作为
QueryResponse
但是如果你得到了像
Accounts
反应如下:
{
"QueryResponse": {
"Account": [
{
"Name": "Accounts Payable (A/P)"
}
],
"startPosition": 1,
"maxResults": 90
},
"time": "2018-08-11T16:14:55.309-07:00"
}
或
Account
或者从已知名单到
Results
我的模型中的属性?
现在我直接在代码中解决它,但我不确定动态转换是否会有很大的性能问题:
var resultObjectJson = JsonConvert.DeserializeObject<dynamic>(body);
var entityName = typeof(T).Name;
var maxResults = (Int64) resultObjectJson.QueryResponse.maxResults;
var startPositions = (Int64) resultObjectJson.QueryResponse.startPosition;
var results = ((JArray) resultObjectJson.QueryResponse[entityName]).ToObject<List<T>>();
var resultRoot = new QueryResponseRoot<T>
{
QueryResponse = new QueryResponse<T>
{
MaxResults = maxResults,
StartPositions = startPositions,
Results = results
}
};
return resultRoot.QueryResponse;