答えはJSONからオンザフライで実行可能なオブジェクトに変換するためにJSONとjオブジェクトにLINQを使用して記述するオブジェクトにNewtonsoft JSONをデシリアライズ。以下は、私のJSONから実行可能なオブジェクトへと私を連れてきた完成コードです。その後に元の質問が続きます。VB.NETは、与えられた動的
'Parse string of JSON data into a JObject that can be accessed via VB.NET
Dim resultSet As JObject = JObject.Parse(responseBody)
'Data can be accessed as seen below
Dim cpu As String = CType(resultSet("KeyName"), String)
=========================================== ==============================
私はと呼ばれるサービスにいくつかのAPI呼び出しを作ることになりますWebアプリケーションを持っています接し(http://www.incontact.com/)
API呼び出しのそれぞれは、このようにフォーマットされたJSONで満たされたHTTPレスポンスを受信します。
{
"resultSet": {
"_links": {
"self": "string",
"next": "string",
"previous": "string"
},
"businessUnitId": 0,
"lastPollTime": "2016-11-08T21:45:46.510Z",
"totalRecords": 0,
"agents": [
{
"agentId": 0,
"userName": "string",
"firstName": "string",
"middleName": "string",
"lastName": "string",
"emailAddress": "string",
"isActive": true,
"teamId": 0,
"teamName": "string",
"reportToId": 0,
"reportToName": "string",
"isSupervisor": true,
"lastLogin": "2016-11-08T21:45:46.510Z",
"lastUpdated": "2016-11-08T21:45:46.510Z",
"location": "string",
"custom1": "string",
"custom2": "string",
"custom3": "string",
"custom4": "string",
"custom5": "string",
"internalId": "string",
"profileId": 0,
"profileName": "string",
"timeZone": "string",
"country": "string",
"countryName": "string",
"state": "string",
"city": "string",
"chatRefusalTimeout": 0,
"phoneRefusalTimeout": 0,
"workItemRefusalTimeout": 0,
"defaultDialingPattern": 0,
"defaultDialingPatternName": "string",
"teamDefaultMaxChats": true,
"maxConcurrentChats": 0,
"notes": "string",
"createDate": "2016-11-08T21:45:46.510Z",
"inactiveDate": "2016-11-08T21:45:46.510Z",
"hireDate": "2016-11-08T21:45:46.510Z",
"terminationDate": "2016-11-08T21:45:46.510Z",
"rehireStatus": true,
"employmentType": 0,
"employmentTypeName": "Full-Time",
"referral": "string",
"atHome": true,
"hiringSource": "string",
"ntLoginName": "string",
"scheduleNotification": "5",
"federatedId": "string",
"sipUser": "string",
"useTeamMaxEmailInboxCount": true,
"maxEmailInboxCount": 0
}
]
}
}
私はNewtoでJSONをデシリアライズしていますnsoft。私は現在、HTTPレスポンスを取り込んで実行可能な.NETオブジェクトを作成するためにJSONをデシリアライズしています
{
"resultSet": {
"businessUnitId": 0,
"lastPollTime": "2016-11-08T21:45:46.604Z",
"teams": [
{
"teamId": 0,
"teamName": "string",
"isActive": true,
"description": "string",
"notes": "string",
"lastUpdateTime": "2016-11-08T21:45:46.604Z",
"inViewEnabled": true,
"wfoEnabled": true,
"wfmEnabled": true,
"qmEnabled": true,
"maxConcurrentChats": 0,
"agentCount": 0,
"maxEmailInboxCount": true,
"inViewGamificationEnabled": true,
"inViewChatEnabled": true,
"inViewLMSEnabled": true,
"analyticsEnabled": true
}
],
"agents": [
{
"agentId": 0,
"firstName": "string",
"lastName": "string"
}
]
}
}
:しかし、それぞれ異なる呼び出しでこのような別のキー/値のペアがあるでしょう。そうするためには、これは、要求が成功したと仮定すると、HTTPリクエストを省略し、私の短縮コードです:
If Not String.IsNullOrEmpty(responseBody) Then
' Success. Do something with the response.
'Declare object for holding the JSON from the API call for this call only (class does not fit other calls)
Dim resultSet As GetAgentsAPICall = New GetAgentsAPICall
'Deserialize JSON response into the new resultSet object
resultSet = JsonConvert.DeserializeObject(responseBody)
問題は、私だけではなく、文字列を取ることができるという、各API呼び出しのための特定のクラスを必要とするということですJSONの書式設定を使用して、キー/値と一致するプロパティを持つオブジェクトにスローします。以下は、私はそれが(長さが長く、リストされた最初のもの)だけで上記のAPIコールで取りしたクラスです。
Public Class GetAgentsAPICall
Public Property resultSet As Resultset
End Class
Public Class Resultset
Public Property _links As _Links
Public Property businessUnitId As Integer
Public Property lastPollTime As Date
Public Property totalRecords As Integer
Public Property agents() As Agent
End Class
Public Class _Links
Public Property self As String
Public Property _next As String
Public Property previous As String
End Class
Public Class Agent
Public Property agentId As Integer
Public Property userName As String
Public Property firstName As String
Public Property middleName As String
Public Property lastName As String
Public Property emailAddress As String
Public Property isActive As Boolean
Public Property teamId As Integer
Public Property teamName As String
Public Property reportToId As Integer
Public Property reportToName As String
Public Property isSupervisor As Boolean
Public Property lastLogin As Date
Public Property lastUpdated As Date
Public Property location As String
Public Property custom1 As String
Public Property custom2 As String
Public Property custom3 As String
Public Property custom4 As String
Public Property custom5 As String
Public Property internalId As String
Public Property profileId As Integer
Public Property profileName As String
Public Property timeZone As String
Public Property country As String
Public Property countryName As String
Public Property state As String
Public Property city As String
Public Property chatRefusalTimeout As Integer
Public Property phoneRefusalTimeout As Integer
Public Property workItemRefusalTimeout As Integer
Public Property defaultDialingPattern As Integer
Public Property defaultDialingPatternName As String
Public Property teamDefaultMaxChats As Boolean
Public Property maxConcurrentChats As Integer
Public Property notes As String
Public Property createDate As Date
Public Property inactiveDate As Date
Public Property hireDate As Date
Public Property terminationDate As Date
Public Property rehireStatus As Boolean
Public Property employmentType As Integer
Public Property employmentTypeName As String
Public Property referral As String
Public Property atHome As Boolean
Public Property hiringSource As String
Public Property ntLoginName As String
Public Property scheduleNotification As String
Public Property federatedId As String
Public Property sipUser As String
Public Property useTeamMaxEmailInboxCount As Boolean
Public Property maxEmailInboxCount As Integer
End Class
私は20または30と同様の長いクラスの事前構築を避けるためにしようとしているが、その代わりに構築します変更可能なリストまたはオブジェクトをオンザフライで表示します。値を取得してデータベースに格納するか、またはそれらを変更し、値をPOSTする別のAPI呼び出しを使用する必要があります。誰かがベストプラクティスを持っているのですか、それとも20-30の巨大なクラス定義に縛られていますか?
ありがとうございました!また、LINQを使用して照会することができますJObject
と
私が必要としていたのは、それを解析して一般化されたオブジェクトにして、アクセスして操作することができるメソッドです。ありがとうございますtrashr0x –
私は助けることができてうれしいです。 – trashr0x