对azure表存储中的数据进行正确的查询和分组,并以正确的嵌套.json格式将其发布到API



我有很多数据存储在一个azure表存储表(字符串数据类型)。

我想张贴只有某些这些字段的API每x分钟在一个。json格式使用一个带有定时器触发器的azure函数。一切都很好,但是:

json的格式目前是包括方括号,我试图删除没有成功:

最重要的是删除括号,所以也许我可以"硬编码"。可以去掉括号吗?我花了很多时间来让它正确的"分组"。格式。有更简单的方法吗?

{
"Test":[
{
"name":"Tom"
}
],
"Info":[
{
"Weight":"122",
"Age":"22"
}
],
"License":[
{
"number":"2222312"
}
]
}

首先:我正在查询azure表存储中的数据:

public async Task Run(
[TimerTrigger("0 */5 * * * *", RunOnStartup = true)] TimerInfo myTimer,
[Table("TestTable", Connection = "STORAGE-ConnectionString")] CloudTable TestTable,
ICollector<string> events,
ILogger log)
{
try
{
TableContinuationToken token = null;
List<PersonEntity> Entities = new List<PersonEntity>();
do
{
var query = new TableQuery<PersonEntity>().Where(TableQuery.GenerateFilterCondition("Status"
, QueryComparisons.Equal, "NEW"));
var result = await TestTable.ExecuteQuerySegmentedAsync(query, token);

Entities.AddRange(result.Results);
token = result.ContinuationToken;

} while (token != null);

其次:我试图用以下代码收集Azure表中的某些字段。

IEnumerable<IGrouping<string, PersonEntity>> groupedEntities = Entities.GroupBy(x => x.PartitionKey);
foreach (IGrouping<string, PersonEntity> group in groupedEntities)
{
PersonEntity baseEntity = group.First();
var personBooking = new PersonBookingModel
{
Test = group.Select(x => new PersonBookingRouteModel 
{
name=x.PersonName.ToString()
}).ToList(),
Info = group.Select(x => new PersonBookingGoodsModel
{
Weight= x.PersonWeight.ToString(),
Age = x.PersonAge.ToString()
}).ToList(),
License = group.Select(x => new PersonBookingDriverModel
{
number = x.LicenceNumber.ToString(),                             
}).ToList()
};

此外:

try
{
var payload = JsonConvert.SerializeObject(personBooking);
using (HttpContent content = new StringContent(payload, Encoding.UTF8, "application/json"))
{
if (payload == "")
{
log.LogWarning("No payload created sucessfully");
}
else {
log.LogWarning(payload); 
}


latestResponse = await httpClient.PostAsync(Environment.GetEnvironmentVariable("API-Endpoint"), content).ConfigureAwait(false);
if (latestResponse.IsSuccessStatusCode)
{
log.LogWarning("Response with endpoint");
}
string data = await latestResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

不带括号的输出需要为:

{
"Test":
{
"name":"Tom"
}
,
"Info":
{
"Weight":"122",
"Age":"22"
}
,
"License":
{
"number":"2222312"
}

}

根据上述评论,我将您的评论转换为答案,以便可以帮助其他社区成员:

因此在将代码更改为-

之后
var res = payload.Replace ("[","").Replace ("]","");

希望您的问题解决了。

相关内容

  • 没有找到相关文章

最新更新