I am using Json.NET to create a json definition whose structure can change. Therefore, I canβt just serialize the class and create the structure on the fly using Json for Linq. I'm having trouble creating the following structure using JObject, JArray, JProperty, etc.
{ 'external_id':'UNIQUE_ID_222222222', 'firstname':'John', 'lastname':'Smith', 'customFields': { 'custom1':'custom1 val', 'custom2':'custom2 val', 'custom3"':'custom3 val' } }
I tried using the following code:
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)
However, when I do this, I get another template that was not accepted by the web service
{[ { "external_id": "50702", "firstname": "John", "lastname": "Smithson", "customFields": [ { "custom1":"custom1 val", "custom2":"custom2 val", "custom3":"custom3 val" } ] } ]}
I thought I should add properties directly to JArray, but this raises a runtime exception.
I saw a similar pattern that is created when deserializing a Dictionary object (String, String), but I really do not want to add my custom fields to the dictionary just to deserialize them. It should be possible to create them using the above notation.