简体   繁体   中英

How to read jagged array

Am calling an API which returns the following array in a string format:

[
  [
    "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? 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.

You can use JSON.Net to deserialize it.

    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

https://dotnetfiddle.net/DXi041

You should use Json deserializer to convert your string to an a jagged array. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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