Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to create a dictionary structure in json.net using json to linq
    primarykey
    data
    text
    <p>I am using Json.NET to create a definition of json whose structure may change. I am therefore unable to simply serialize a class and am creating the structure on the fly using Json to Linq. I am having issues creating the following structure using JObject, JArray, JProperty, etc</p> <pre><code> { 'external_id':'UNIQUE_ID_222222222', 'firstname':'John', 'lastname':'Smith', 'customFields': { 'custom1':'custom1 val', 'custom2':'custom2 val', 'custom3"':'custom3 val' } } </code></pre> <p>I tried using the following code:</p> <pre><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) </code></pre> <p>However when I do this I get a different pattern that was not accepted by the webservice</p> <pre><code>{[ { "external_id": "50702", "firstname": "John", "lastname": "Smithson", "customFields": [ { "custom1":"custom1 val", "custom2":"custom2 val", "custom3":"custom3 val" } ] } ]} </code></pre> <p>I thought that I should be adding properties directly to the JArray but doing this causes a run time exception.</p> <p>I have seen a similar pattern that is created when you deserialize a Dictionary(String, String) object but I don't really want to add my custom fields to a dictionary only to then deserialize them. It must be possible to create them using the above notation.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload