在 C# 中为来自 Salesforce REST API 的嵌套 JSON 数据设置 DataContract
Setting up DataContract in C# for nested JSON data from Salesforce REST API
我需要帮助弄清楚如何将 JSON 数据导入我的 C# 应用程序,该应用程序从 Salesforce REST API.
获取数据
首先,我有一些 JSON 数据如下所示:
{
"attributes" : {
"type" : "Object__c",
"url" : "/services/data/v34.0/sobjects/Object__c/a0FF000000CaEggEMA"
},
"Id" : "a0FF000000CaEggEMA",
"Advertising__c" : 1720.0
}
而且我能够创建一个 class 和 DataContract 如下:
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
namespace MyNamespace
{
[DataContract]
public class Response
{
[DataMember(Name = "Advertising__c")]
public decimal decAdvertising { get; set; }
}
}
而且,我能够使用以下代码将数据导入我的程序并访问数据...
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;
}
}
但是,在上面的示例中,每个 JSON 响应只有一个 Id
和一个 Advertising__c
。所以 DataContract 似乎工作正常。
现在我想做的是从以下 JSON:
中获取 collection/list/array(我可以迭代的任何内容)项目
{
"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
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; }
}
现在我只得到两个字段的 NULLs
。事实上,我实际上需要 done
字段,所以我知道是否返回并获取另一批数据。
在一个完美的世界中,我想要某种 collection/list/array 来遍历并获取 strId
,然后将 strId
返回到代码中顶部以获取每个条目的详细信息。
这里有什么帮助吗?
您的 Response2
class 应如下所示:
public class Response2
{
[DataMember(Name = "done")]
public bool done;
[DataMember(Name = "records")]
public List<Response3> r3entry;
}
我需要帮助弄清楚如何将 JSON 数据导入我的 C# 应用程序,该应用程序从 Salesforce REST API.
获取数据首先,我有一些 JSON 数据如下所示:
{
"attributes" : {
"type" : "Object__c",
"url" : "/services/data/v34.0/sobjects/Object__c/a0FF000000CaEggEMA"
},
"Id" : "a0FF000000CaEggEMA",
"Advertising__c" : 1720.0
}
而且我能够创建一个 class 和 DataContract 如下:
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
namespace MyNamespace
{
[DataContract]
public class Response
{
[DataMember(Name = "Advertising__c")]
public decimal decAdvertising { get; set; }
}
}
而且,我能够使用以下代码将数据导入我的程序并访问数据...
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;
}
}
但是,在上面的示例中,每个 JSON 响应只有一个 Id
和一个 Advertising__c
。所以 DataContract 似乎工作正常。
现在我想做的是从以下 JSON:
中获取 collection/list/array(我可以迭代的任何内容)项目{
"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
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; }
}
现在我只得到两个字段的 NULLs
。事实上,我实际上需要 done
字段,所以我知道是否返回并获取另一批数据。
在一个完美的世界中,我想要某种 collection/list/array 来遍历并获取 strId
,然后将 strId
返回到代码中顶部以获取每个条目的详细信息。
这里有什么帮助吗?
您的 Response2
class 应如下所示:
public class Response2
{
[DataMember(Name = "done")]
public bool done;
[DataMember(Name = "records")]
public List<Response3> r3entry;
}