C# 操作JSON字符串用于与网页中的返回数据进行互动

本文详细介绍了在没有Visual Studio的情况下,如何通过命令行使用csc编译器添加.NET DLL引用,并解释了如何将JSON字符串转换为JArray进行遍历解析的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**
* if you use combile by csc in cmd, use follow str
* csc /reference:Newtonsoft.json.dll JsonString.cs
* pls make sure "Newtonsoft.json.dll" is in the same dir
*/

using System;

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace JsonString{
class JsonString{
static void Main(){
string str = "[11,22,33,44,55]";

JArray jArray = (JArray)JsonConvert.DeserializeObject(str);

for(int i=0;i<jArray.Count;i++){
System.Console.WriteLine(jArray[i]);
}
}
}
}

值得一提的是,通常情况下是在VS中进行DLL添加引用的,而这里因为电脑的原因没有安装VS,因此是在命令行下直接进行的编译,为此不得不提一下在命令行下添加引用的办法

csc /reference:x.dll x.cs

这样,使用相对路径就可以对文件进行编译了

### 使用C#帮助类在Azure Functions中DataVerse进行交互 为了使Azure Functions能够Dataverse高效互动,开发者可以利用微软提供的SDK和其他工具来简化开发过程。具体来说,Power Platform SDK提供了访问Dataverse数据和服务的能力。 #### 安装必要的NuGet包 首先,在项目中安装`Microsoft.PowerPlatform.DataApi.Client.Net` NuGet包,这使得调用Dataverse Web API变得简单[^1]: ```bash dotnet add package Microsoft.PowerPlatform.DataApi.Client.Net ``` #### 创建Azure Function应用并配置连接字符串 确保应用程序设置中有正确的Dataverse环境URL以及身份验证所需的凭据信息。这些参数可以在Azure Portal中的函数应用配置里设定。 #### 编写Function代码示例 下面是一个简单的HTTP触发器的Azure Function例子,该函数会查询特定实体的数据记录: ```csharp using System; using System.IO; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.Azure.WebJobs; using Microsoft.Azure.WebJobs.Extensions.Http; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using Microsoft.Xrm.Sdk; // Part of Power Platform SDK using Microsoft.Cds.Sdk.Proxy; public static class DataverseInteractionFunction { [FunctionName("FetchAccountRecords")] public static async Task<IActionResult> Run( [HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req, ILogger log) { var orgServiceProxy = new OrganizationServiceProxy(new Uri(Environment.GetEnvironmentVariable("DataverseUrl")), Environment.GetEnvironmentVariable("ClientId"), Environment.GetEnvironmentVariable("ClientSecret")); IOrganizationService service = (IOrganizationService)orgServiceProxy; string fetchXml = @"<fetch> <entity name='account'> <attribute name='name' /> <order attribute='name' descending='false' /> </entity> </fetch>"; EntityCollection result = await service.RetrieveMultipleAsync(new FetchExpression(fetchXml)); return new OkObjectResult(result.Entities); } } ``` 此段代码展示了如何初始化服务代理实例,并执行一次针对`account`表的检索操作。注意这里使用的是异步方法`RetrieveMultipleAsync()`来进行非阻塞式的API请求处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值