-1

I have a salesforce rest service which returns results in IEnumerable format. Below is a sample results.

[{
  "attributes": {
    "type": "Account",
    "url": "/services/data/v28.0/sobjects/Account/001i0000WK5xYAAT"
  },
  "RecordType": {
    "attributes": {
      "type": "RecordType",
      "url": "/services/data/v28.0/sobjects/RecordType/012i00000x7FwAAI"
    },
    "Name": "Health Care Practitioners"
  },
  "Name": "JOSEPH SANDERS",
  "Status_ims__c": "Verified",
},
{
  "attributes": {
    "type": "Account",
    "url": "/services/data/v28.0/sobjects/Account/001i000000WK5xYAAT"
  },
  "RecordType": {
    "attributes": {
      "type": "RecordType",
      "url": "/services/data/v28.0/sobjects/RecordType/012i0000000x7FwAAI"
    },
    "Name": "Health Care Practitioners"
  },
  "Name": "DONALD GRABER",
  "Status_ims__c": "Verified",
}]


public class Account
    {

        public string Name { get{ return GetOption ("Name");} }
        public string Status_ims__c { get{ return GetOption ("Status_ims__c");}}
        public Attributes attributes {get;}
        public RecordType recordType {get;}

    }

    public class Attributes
    {
        public string type { get; set; }
        public string url { get; set; }
    }

    public class Attributes2
    {
        public string type { get; set; }
        public string url { get; set; }
    }

    public class RecordType
    {
        public Attributes2 attributes { get; set; }
        public string Name { get; set; }
    }

Above is the structure I have for Account Object. How to convert results to List and map to each property on Account object.

user360
  • 350
  • 3
  • 9
  • You should use a third party library. `Json.NET` seems to be pretty popular. They will have tutorials. – nvoigt May 11 '15 at 05:06
  • 1
    Probable duplicate here: http://stackoverflow.com/questions/1212344/parse-json-in-c-sharp. I refrain from closing this as a duplicate, only because you may not even be aware you're parsing JSON and so need more help than just knowing how to parse JSON. But please feel free to close your own question as a duplicate if that link answers it. – Peter Duniho May 11 '15 at 06:13

3 Answers3

0

For Example is using json.net ->

var account = JsonConvert.DeserializeObject<List<Account>>(stringData);

Also if there are some differences between the object and the data you can use JsonProperty Annotations

Therefore you could also have more readable properties in your class model like

[JsonProperty("Status_ims__c ")]
public string Status
Boas Enkler
  • 12,264
  • 16
  • 69
  • 143
0

If you use Newtonsoft.Json you can use Newtonsoft.Json.JsonConvert.DeserializeObject method.

Also make sure that your properties have setters in Account class.

serdar
  • 1,564
  • 1
  • 20
  • 30
0

You can simple use this:

var accounts = JsonConvert.DeserializeObject<List<Account>>("your json string...");
Arin Ghazarian
  • 5,105
  • 3
  • 23
  • 21