Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从JSON中的数组数组中读取所有值?_C#_Json_Json.net - Fatal编程技术网

C# 如何从JSON中的数组数组中读取所有值?

C# 如何从JSON中的数组数组中读取所有值?,c#,json,json.net,C#,Json,Json.net,社区 我想从这个json读取所有的值。我用的是C#。我如何做到这一点?我正在尝试使用openroute服务距离矩阵。 我的想法是: 使用生成器/转换器生成Json类,例如: 示例代码: public Form1() { InitializeComponent(); string jsonData = File.ReadAllText(@"c:\root\t01.json"); Root root = JsonConvert.DeserializeO

社区

我想从这个json读取所有的值。我用的是C#。我如何做到这一点?我正在尝试使用openroute服务距离矩阵。 我的想法是:


使用生成器/转换器生成Json类,例如:

示例代码:

public Form1()
{
    InitializeComponent();

    string jsonData = File.ReadAllText(@"c:\root\t01.json");
    Root root  = JsonConvert.DeserializeObject<Root>(jsonData);
}

public class Root    {
    public List<List<double>> distances { get; set; } 
    public List<Destination> destinations { get; set; } 
    public List<Source> sources { get; set; } 
    public Metadata metadata { get; set; } 
}

public class Destination    {
    public List<double> location { get; set; } 
    public double snapped_distance { get; set; } 
}

public class Source    {
    public List<double> location { get; set; } 
    public double snapped_distance { get; set; } 
}

public class Query    {
    public List<List<double>> locations { get; set; } 
    public string profile { get; set; } 
    public string responseType { get; set; } 
    public List<string> metricsStrings { get; set; } 
    public List<string> metrics { get; set; } 
    public string units { get; set; } 
}

public class Engine    {
    public string version { get; set; } 
    public DateTime build_date { get; set; } 
    public DateTime graph_date { get; set; } 
}

public class Metadata    {
    public string attribution { get; set; } 
    public string service { get; set; } 
    public long timestamp { get; set; } 
    public Query query { get; set; } 
    public Engine engine { get; set; } 
}
public Form1()
{
初始化组件();
字符串jsonData=File.ReadAllText(@“c:\root\t01.json”);
Root=JsonConvert.DeserializeObject(jsonData);
}
公共类根{
公共列表距离{get;set;}
公共列表目标{get;set;}
公共列表源{get;set;}
公共元数据{get;set;}
}
公务舱目的地{
公共列表位置{get;set;}
公共双快照_距离{get;set;}
}
公共类源{
公共列表位置{get;set;}
公共双快照_距离{get;set;}
}
公共类查询{
公共列表位置{get;set;}
公共字符串配置文件{get;set;}
公共字符串响应类型{get;set;}
公共列表度量字符串{get;set;}
公共列表度量{get;set;}
公共字符串单位{get;set;}
}
公共级引擎{
公共字符串版本{get;set;}
公共日期时间生成日期{get;set;}
公共日期时间图_date{get;set;}
}
公共类元数据{
公共字符串属性{get;set;}
公共字符串服务{get;set;}
公共长时间戳{get;set;}
公共查询查询{get;set;}
公共引擎引擎{get;set;}
}

我认为“保存”不是最好的词。您似乎想要从距离数组中的数组中提取所有值。请将您的JSON作为文本而不是图像添加到问题中-顺便说一句,这不是JSON。您尝试过您的想法吗?它起作用了吗?什么不起作用?请你也回答Liam的问题好吗?@Jochem Van Hespen好吧,我只是部分地读了你的评论:经常有人建议使用Visual Studio的
Paste Special->Paste JSON作为类
。我指的是这个工具。我不确定Visual Studio代码建议是否适用于这里。
{
    "distances": [[0, 108628.26, 2669713, 11595025], [108952.97, 0, 2674477.75, 11609762], [2688555, 2681139.5, 0, 8405009], [11596626, 11611808, 8734262, 0]],
    "destinations": [
        {
            "location": [9.700817, 48.476406],
            "snapped_distance": 118.92
        },
        {
            "location": [9.207773, 49.153882],
            "snapped_distance": 10.54
        },
        {
            "location": [37.572963, 55.801279],
            "snapped_distance": 17.45
        },
        {
            "location": [115.665017, 38.100717],
            "snapped_distance": 648.79
        }
    ],
    "sources": [
        {
            "location": [9.700817, 48.476406],
            "snapped_distance": 118.92
        },
        {
            "location": [9.207773, 49.153882],
            "snapped_distance": 10.54
        },
        {
            "location": [37.572963, 55.801279],
            "snapped_distance": 17.45
        },
        {
            "location": [115.665017, 38.100717],
            "snapped_distance": 648.79
        }
    ],
    "metadata":
    {
        "attribution": "openrouteservice.org | OpenStreetMap contributors",
        "service": "matrix",
        "timestamp": 1603348601609,
        "query":
        {
            "locations": [[9.70093, 48.477473], [9.207916, 49.153868], [37.573242, 55.801281], [115.663757, 38.106467]],
            "profile": "cycling-road",
            "responseType": "json",
            "metricsStrings": ["DISTANCE"],
            "metrics": ["distance"],
            "units": "m"
        },
        "engine":
        {
            "version": "6.3.0",
            "build_date": "2020-10-19T02:01:48Z",
            "graph_date": "2020-10-12T06:58:07Z"
        }
    }
}
public Form1()
{
    InitializeComponent();

    string jsonData = File.ReadAllText(@"c:\root\t01.json");
    Root root  = JsonConvert.DeserializeObject<Root>(jsonData);
}

public class Root    {
    public List<List<double>> distances { get; set; } 
    public List<Destination> destinations { get; set; } 
    public List<Source> sources { get; set; } 
    public Metadata metadata { get; set; } 
}

public class Destination    {
    public List<double> location { get; set; } 
    public double snapped_distance { get; set; } 
}

public class Source    {
    public List<double> location { get; set; } 
    public double snapped_distance { get; set; } 
}

public class Query    {
    public List<List<double>> locations { get; set; } 
    public string profile { get; set; } 
    public string responseType { get; set; } 
    public List<string> metricsStrings { get; set; } 
    public List<string> metrics { get; set; } 
    public string units { get; set; } 
}

public class Engine    {
    public string version { get; set; } 
    public DateTime build_date { get; set; } 
    public DateTime graph_date { get; set; } 
}

public class Metadata    {
    public string attribution { get; set; } 
    public string service { get; set; } 
    public long timestamp { get; set; } 
    public Query query { get; set; } 
    public Engine engine { get; set; } 
}