在C#中为来自Salesforce REST API的嵌套JSON数据设置DataContract

时间:2015-10-27 20:23:28

标签: c# json rest salesforce

我需要帮助找出如何将JSON数据放入我的C#应用​​程序中,该应用程序从Salesforce REST API中获取数据。

首先,我有一些看起来像这样的JSON数据:

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

namespace MyNamespace

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

}

我能够使用DataContract创建一个类,如下所示:

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;
    }
}

并且,我能够使用以下代码将数据导入我的程序并访问数据......

Id

但是,在上面的示例中,每个JSON响应只有一个Advertising__c和一个{ "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" } ] } 。所以DataContract似乎工作正常。

现在我想做的是从以下JSON获取一个集合/列表/数组(我可以迭代的任何东西):

DataContract

我尝试将以下代码添加到[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; } } 类:

NULLs

现在我正在为这两个字段获取done。事实上,我实际上需要strId字段,所以我知道是否要返回并获取另一批数据。

在一个完美的世界中,我想获得某种集合/列表/数组来迭代并抓住strId,然后将{"object":"page","entry":[{"id":"593349087454592","time":1445698004,"changes":[{"field":"feed","value":{"item":"post","verb":"add","post_id":"593349087454592_787861828003316","is_hidden":false,"sender_id":1442769135993502,"message":"Post #4","sender_name":"Sheva Che"}}]}]}反馈到代码中顶部以获取每个条目的详细信息。

这里有任何帮助吗?

1 个答案:

答案 0 :(得分:3)

您的Response2课程应如下所示:

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