1

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

David
  • 208,112
  • 36
  • 198
  • 279
  • Welcome to Stack Overflow! You are encouraged to make an attempt to write your code. If you encounter a specific technical problem during that attempt, such as an error or unexpected result, we can help with that. Please provide specific information about that attempt and what didn't work as expected. To learn more about this community and how we can help you, please start with the [tour] and read [ask] and its linked resources. – David Mar 22 '23 at 12:16
  • I *suspect* the terminology you're missing is "deserializing" your JSON response into an object structure. – David Mar 22 '23 at 12:18

1 Answers1

0

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;
Serge
  • 40,935
  • 4
  • 18
  • 45
  • small correction DataTable dt = new JArray(JObject.Parse(result).SelectToken("records") .Select(x => new JObject(((JObject)x).Properties() .Where(y => y.Value.Type != JTokenType.Object)))) .ToObject(); var source = new BindingSource(); source.DataSource = dt; grid.DataSource = source; – Ahmed Yousry Mar 23 '23 at 17:25
  • @AhmedYousry What have you changed? All I can see that you renamed "json" to "result". – Serge Mar 23 '23 at 20:04
  • y => y.Value.Type ! – Ahmed Yousry Mar 25 '23 at 00:47