gpt4 book ai didi

c# - 在Azure函数中解析C#中的Json

转载 作者:行者123 更新时间:2023-12-03 04:46:26 25 4
gpt4 key购买 nike

就我而言,我收到一个包含多行的 Json,在 Azure 中采用这种格式。我收到的行数是可变的。我收到的变量数量也是可变的(最多可以有 20 个)

{"Data":[
{"name":"Variable A","value":0.321721,"timecreated":"2018-1-15T11:10:7.977Z"},
{"name":"Variable B","value":-8.932533,"timecreated":"2018-1-15T11:10:8.17Z"},
{"name":"Variable C","value":-7.068326,"timecreated":"2018-1-15T11:10:8.58Z"},
{"name":"Variable A","value":-3.580420,"timecreated":"2018-1-15T11:10:8.98Z"},
{"name":"Variable C","value":1.549976,"timecreated":"2018-1-15T11:10:7.977Z"},
{"name":"Variable A","value":-8.701625,"timecreated":"2018-1-15T11:10:8.17Z"}]}

我想使用类似的东西:

#r "Newtonsoft.Json"
using Newtonsoft.Json;

public class Variables
{
public string name="";
public string value="";
public string timecreated="";

}


public static void Run(Stream myBlob, string name, TraceWriter log)
{
log.Info($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");
var serializer = new JsonSerializer();

using (var sr = new StreamReader(myBlob))
using (var jsonTextReader = new JsonTextReader(sr))
{
var Values= (Variables)serializer.Deserialize(jsonTextReader,typeof(Variables));

// Do something with Values.
}
}

作为变量数组,之后我可以处理 DDBB。另外,多个字典列表也是一个很好的解决方案......有什么想法吗?

最佳答案

您的 JSON 字符串不包含变量数组,它包含一个其 Data 属性是变量数组的对象。

要反序列化它,请创建一个具有 Data 属性的对象:

public class Variable
{
public string name{get;set;}
public string value {get;set;}
public string timecreated {get;set;}
}

public class MyDTO
{
public Variable[] Data{get;set;}
}

反序列化字符串只需一次调用:

var dto=JsonConvert.DeserializeObject<MyDTO>(json);
var values=dto.Data;

关于c# - 在Azure函数中解析C#中的Json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48537804/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com