Search code examples
c#jsonsalesforcesoql

How can I convert curl response from salesforce CRM into datagridview on c#?


Below is a curl response from salesforce CRM , would like to show Id, Status, Brand__c, Case_Name__c on DataGridView using c#

{
    "totalSize": 2,
    "done": true,
    "records": [{
        "attributes": {
            "type": "Case",
            "url": "/services/data/v52.0/sobjects/Case/5001r00002e1X48AAE"
        },
        "Id": "5001r00002e1X48AAE",
        "Status": "Completed",
        "Brand__c": "a0D1r0000178jlfEAA",
        "Case_Name__c": "efg-456"
    }, {
        "attributes": {
            "type": "Case",
            "url": "/services/data/v52.0/sobjects/Case/5001r00002e1XAKAA2"
        },
        "Id": "5001r00002e1XAKAA2",
        "Status": "Completed",
        "Brand__c": "a0D1r0000165952EAA",
        "Case_Name__c": "abc-123"
    }]
}

Tring to find the answer


Solution

  • You can create a DataTable instance and assign it as a data source of your gridview

        DataTable dt = new JArray(JObject.Parse(json).SelectToken("records")
                      .Select(x => new JObject(((JObject)x).Properties()
                      .Where(x => x.Value.Type != JTokenType.Object))))
                      .ToObject<DataTable>();
    
       var source = new BindingSource();
       source.DataSource = dt;
       grid.DataSource = source;