0

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?

2
  • 1
    "records" is a JSON array, so you need to deserialize it to a collection, say public List<Response3> r3entry; Commented Oct 27, 2015 at 20:28
  • @dbc - When I use that syntax, I get: The type or namespace name 'List<Response3>' could not be found (are you missing a using directive or an assembly reference?) Commented Oct 27, 2015 at 21:01

1 Answer 1

3

Your Response2 class should look like this:

public class Response2
{
    [DataMember(Name = "done")]
    public bool done;
    [DataMember(Name = "records")]
    public List<Response3> r3entry;
}
Sign up to request clarification or add additional context in comments.

2 Comments

So when I change this, I get: The type or namespace name 'List<Response3>' could not be found (are you missing a using directive or an assembly reference?)
You need to add using System.Collections.Generic; at the top so you can use List<>.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.