gotmike
gotmike

Reputation: 1615

Setting up DataContract in C# for nested JSON data from Salesforce REST API

I need help figuring out how to get JSON data into my C# application which is grabbing data from the Salesforce REST API.

First, I had some JSON data that looked like this:

{
  "attributes" : {
    "type" : "Object__c",
    "url" : "/services/data/v34.0/sobjects/Object__c/a0FF000000CaEggEMA"
  },
  "Id" : "a0FF000000CaEggEMA",
  "Advertising__c" : 1720.0
}

And I was able to create a class with a DataContract as follows:

using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;

namespace MyNamespace

{
    [DataContract]
    public class Response
    {
        [DataMember(Name = "Advertising__c")]
        public decimal decAdvertising { get; set; }
    }

}

And, I was able to use the following code to get the data into my program and access the data...

public static Response MakeRequest(string requestUrl)
{
    try
    {
        HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
        request.Headers["X-PrettyPrint"] = "1";
        request.Headers["Authorization"] = "OAuth [OAuthKeyGoesHere]";
        using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
        {
            if (response.StatusCode != HttpStatusCode.OK)
                throw new Exception(String.Format(
                "Server error (HTTP {0}: {1}).",
                response.StatusCode,
                response.StatusDescription));
            DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(Response));
            object objResponse = jsonSerializer.ReadObject(response.GetResponseStream());
            Response jsonResponse = objResponse as Response;
            return jsonResponse;
        }
    }
    catch (Exception e)
    {
        System.Windows.Forms.MessageBox.Show(e.Message);
        return null;
    }
}

However, in the above example, there is only one Id and one Advertising__c per JSON response. So the DataContract seems to work fine.

Now what I want to do is get a collection/list/array (anything I can iterate through) of items from the following JSON:

{
  "totalSize" : 4,
  "done" : true,
  "nextRecordsUrl" : "/services/data/v34.0/query/a1gF000003uAsG6IaO-2000",
  "records" : [ {
    "attributes" : {
      "type" : "Object__c",
      "url" : "/services/data/v34.0/sobjects/Object__c/a0FF000000FdAc6MAD"
    },
    "Id" : "a0FF000000FdAc6MAD",
    "Name" : "Name 1"
  }, {
    "attributes" : {
      "type" : "Object__c",
      "url" : "/services/data/v34.0/sobjects/Object__c/a0FF000000lkUJMATg"
    },
    "Id" : "a0FF000000lkUJMATg",
    "Name" : "Name 2"
  }, {
    "attributes" : {
      "type" : "Object__c",
      "url" : "/services/data/v34.0/sobjects/Object__c/a0FF000000CHaPkkMA"
    },
    "Id" : "a0FF000000CHaPkkMA",
    "Name" : "Name 3"
  }, {
    "attributes" : {
      "type" : "Object__c",
      "url" : "/services/data/v34.0/sobjects/Object__c/a0FF000000CIoiUZMA"
    },
    "Id" : "a0FF000000CIoiUZMA",
    "Name" : "Name 4"
  } ]
}

And I tried adding the following code to the DataContract class:

[DataContract]
public class Response2
{
    [DataMember(Name = "records")]
    public Response3 r3entry;
}
[DataContract]
public class Response3
{
    [DataMember(Name = "Id")]
    public string strId { get; set; }
    [DataMember(Name = "Name")]
    public string strName { get; set; }
}

And now I'm just getting NULLs for both fields. In fact, I actually need the done field to so I know whether to go back and get another batch of data.

In a perfect world, I'd like to get some sort of collection/list/array to iterate through and grab the strId, then feed that strId back into the code at the top to get details on each individual entry.

Any help here?

Upvotes: 0

Views: 1167

Answers (1)

Racil Hilan
Racil Hilan

Reputation: 25351

Your Response2 class should look like this:

public class Response2
{
    [DataMember(Name = "done")]
    public bool done;
    [DataMember(Name = "records")]
    public List<Response3> r3entry;
}

Upvotes: 3

Related Questions