C# 将JSON数据插入dynamics中的字段(例如联系人的名字)
我将以下JSON数据存储在外部数据库中:C# 将JSON数据插入dynamics中的字段(例如联系人的名字),c#,json,dynamics-crm,C#,Json,Dynamics Crm,我将以下JSON数据存储在外部数据库中: { "id": "emulator:conversation4acee870-4bf7-11e9-849f-abbeb92fb354|livechat", "botId": "05682e70-4410-11e9-bd6d-d3b4ec617e06", "channelId": "emulator", "conversationId": "4acee870-4bf7-11e9-849f-abbeb92fb354|livechat", "user
{
"id": "emulator:conversation4acee870-4bf7-11e9-849f-abbeb92fb354|livechat",
"botId": "05682e70-4410-11e9-bd6d-d3b4ec617e06",
"channelId": "emulator",
"conversationId": "4acee870-4bf7-11e9-849f-abbeb92fb354|livechat",
"userId": "72aee04c-8ce5-4c1e-abfe-32680a5f2404",
"data": {
"FirstName": "sam",
"Surname": "smith"
},
"etag":"2355667676",
"attachement":"222445666"
}
我想做的是获取firstname的值并将其放入dynamics新联系人实体中
目前,我已经创建了一个控制台应用程序,并已连接到我的外部数据库和dynamics 365。从控制台应用程序到dynamics的连接正常。
我能够查询数据库并检索请求值。但是,我不知道如何在dynamics中插入JSON数据
class Program
{
public static IOrganizationService axoService;
public static IOrganizationService service;
private Guid _accountId;
public Guid _contactId;
//connect to cosmos db
private const string EndpointUrl = "https://botstore.documents.azure.com:###/";
private const string PrimaryKey = "Z###########################=";
private DocumentClient client;
static void Main(string[] args)
{
Task.Run(async () =>
{
var endpoint = EndpointUrl;
var masterKey = PrimaryKey;
using (var client = new DocumentClient(new Uri(endpoint), masterKey))
{
Console.WriteLine(">>>> Querying Document <<<<<<");
//returns all
var response = client.CreateDocumentQuery(UriFactory.CreateDocumentCollectionUri("botdb","botcollection"),"select * from c").ToList();
//returns first one
var g = response.First();
Console.WriteLine($"id:{g.id}");
//return botid contains convo
var p = response.Where(op => op.id.Contains("conv")).ToList().First();
Console.WriteLine($"id2:{p.id}");
Console.WriteLine($"id2:{p.botId}");
Console.WriteLine($"id2:{p.data.FirstName}");
Console.WriteLine("Query done");
//After query delete conversation? maybe after it is added to dynamics, then delete in another function
var connectionString = @"AuthType = Office365; Url = https://#####.crm11.dynamics.com/;Username=s######@dynamics.co.uk;Password=########";
CrmServiceClient conn = new CrmServiceClient(connectionString);
axoService = conn.OrganizationServiceProxy;
IOrganizationService _orgService;
orgService = (IOrganizationService)conn.OrganizationWebProxyClient != null ? (IOrganizationService)conn.OrganizationWebProxyClient : (IOrganizationService)conn.OrganizationServiceProxy;
RetrieveVersionRequest versionRequest = new RetrieveVersionRequest();
RetrieveVersionResponse versionResponse =(RetrieveVersionResponse)_orgService.Execute(versionRequest);
Console.WriteLine("Microsoft Dynamics CRM version {0}.",versionResponse.Version);
var pop = p.data.FirstName;//gets the value from json
data//sam
Entity cont = new Entity("contact");
cont["firstname"] = pop;//error:Type **'Newtonsoft.Json.Linq.JToken' is a recursive collection data contract which is not supported. Consider modifying the definition of collection 'Newtonsoft.Json.Linq.JToken' to remove references to itself.**- this is not WCF though so don't have service references
cont["lastname"] = "Simmmmmmspsson";
axoService.Create(cont);
Console.WriteLine("new contact added");
Console.ReadLine();
如何在Microsoft Dynamics中联系人实体的FirstName字段中插入JSON数据?您编写的代码是
cont["firstname"] = pop;
因此,在上面的一行中,pop可能包含数据,但其类型是Newtonsoft.Json.Linq.JToken,而不是纯字符串
但是你的合同实体需要纯字符串
这就是为什么它会抛出这样的错误
类型**'Newtonsoft.Json.Linq.JToken'是不受支持的递归收集数据协定。考虑修改集合“NeNethSoop.JSO.LINQ,JToken”的定义,以删除对自身的引用。
因此,如果您将类型从JToken更改为string,那么您的异常可能会消失
var pop = p.data.FirstName.ToString();
或
您是否尝试使用DataContractJsonSerializer来序列化和反序列化JSON?或者根据需要使用来序列化和反序列化JSON。请参见以下示例:。我正在使用Newtonsoft JSON包。NET@Sam,试试这个=>var pop=p.data.FirstName.ToString;让我知道:@Sam,这里的假设是JSON字符串将来自您的数据库,但将具有我创建的模型类型。基本上JSON的结构应该是相同的。
var pop = Convert.ToString(p.data.FirstName);