Json.NETを使用して、構造が変更される可能性のあるjsonの定義を作成しています。したがって、クラスを単純にシリアル化することはできず、JsonからLinqを使用してその場で構造を作成しています。JObject、JArray、JPropertyなどを使用して次の構造を作成する際に問題が発生します
{
'external_id':'UNIQUE_ID_222222222',
'firstname':'John',
'lastname':'Smith',
'customFields':
{
'custom1':'custom1 val',
'custom2':'custom2 val',
'custom3"':'custom3 val'
}
}
次のコードを使用してみました。
Dim json As New JArray()
Dim jsonObj As New JObject( _
New JProperty("external_id", "UNIQUE_ID_222222222"),
New JProperty("firstname", "John"),
New JProperty("lastname", "Smith"))
Dim jsonCustomFields As New JArray
Dim jsonCustomObject As New JObject
jsonCustomFields.Add(jsonCustomObject)
For Each field In CustomFieldList
jsonCustomObject.Add(New JProperty(field.Label, field.Value))
Next
jsonObj.Add(New JProperty("customFields", jsonCustomFields))
json.Add(jsonContrib)
ただし、これを行うと、Webサービスで受け入れられなかった別のパターンが表示されます
{[
{
"external_id": "50702",
"firstname": "John",
"lastname": "Smithson",
"customFields": [
{
"custom1":"custom1 val",
"custom2":"custom2 val",
"custom3":"custom3 val"
}
]
}
]}
プロパティをJArrayに直接追加する必要があると思いましたが、これを行うと実行時例外が発生します。
Dictionary(String、String)オブジェクトを逆シリアル化するときに作成される同様のパターンを見てきましたが、カスタムフィールドを辞書に追加して、それらを逆シリアル化するだけではありません。上記の表記法を使用してそれらを作成できる必要があります。