简体   繁体   English

如何读取锯齿状数组

[英]How to read jagged array

Am calling an API which returns the following array in a string format: 我正在调用一个API,该API以字符串格式返回以下数组:

[
  [
    "9200bc80bff0432081d01d103940ffb0",
    "HelloEarth",
    "https://www.google.com",
    "invalid domain",
    "",
    ""
  ],
  [
    "6f269d6627624d61836d1d60b268ff6b",
    "HelloPluto",
    "yahoo",
    "72f988bf86f141af91ab2d7cd011db47",
    "11/30/2015 12:00:00 AM",
    ""
  ],
  [
    "6f269d6627624d61836d1d60b268ff6b",
    "HelloMars",
    "bing",
    "APIClient",
    "11/30/2015 12:00:00 AM",
    ""
  ]
]

My question is how to convert this string to Array? 我的问题是如何将此字符串转换为Array? And If i want to read only the first element of first array, how to do it? 如果我只想读取第一个数组的第一个元素,该怎么做? i have attached only simplified string which actually contains more than than 3 arrays. 我只附加了实际上包含三个以上数组的简化字符串。 but each array contains only six elements. 但每个数组仅包含六个元素。

I don't know if it was intentional, but that "string format" is valid JSON. 我不知道这是否是故意的,但该“字符串格式”是有效的JSON。

You can use JSON.Net to deserialize it. 您可以使用JSON.Net反序列化它。

    var data = "[[\"9200bc80bff0432081d01d103940ffb0\", \"HelloEarth\", \"https://www.google.com\", \"invalid domain\", \"\", \"\"],[\"6f269d6627624d61836d1d60b268ff6b\", \"HelloPluto\", \"yahoo\", \"72f988bf86f141af91ab2d7cd011db47\", \"11/30/2015 12:00:00 AM\", \"\"],[\"6f269d6627624d61836d1d60b268ff6b\", \"HelloMars\", \"bing\", \"APIClient\", \"11/30/2015 12:00:00 AM\", \"\"]]";
    var array = Newtonsoft.Json.JsonConvert.DeserializeObject<string[][]>(data);

    Console.WriteLine(array[0][0]);

Output: 9200bc80bff0432081d01d103940ffb0 输出: 9200bc80bff0432081d01d103940ffb0

https://dotnetfiddle.net/DXi041 https://dotnetfiddle.net/DXi041

You should use Json deserializer to convert your string to an a jagged array. 您应该使用Json解串器将字符串转换为锯齿状数组。 The you can access to your array as a regular array just taking in count that every position that it returns contains an array, and if you want to access to a specific position you need an index again. 您可以将其作为常规数组访问数组,只需计算返回的每个位置都包含一个数组即可,如果要访问特定位置,则需要再次创建索引。

var a = jagged[firstIndex]; //returns an array

var obj = a[secondIndex]; //get the object inside the array

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM