How can I convert object to array
Public async list
{
// here I need to convert obj to array
}
And need to count array count
How can I convert object to array
Public async list
{
// here I need to convert obj to array
}
And need to count array count
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Muhammad Imran AnsariRank #326 · 7.6K reputationPosted Aug 7, 2023, 4:19 PM EDT
To achieve this, first determine the type of the object and then convert it accordingly. Since the method signature you provided is for a list of strings, Here's how you can do it:
public async Task ConvertObjToArray(object obj) { if (obj is null) { return new string[0]; // Return an empty array if the object is null } if (obj is string str) // Check if the object is already a string { return new string[] { str }; // Convert the string to a string array with one element } if (obj is IEnumerable stringList) // Check if the object is a collection of strings { return stringList.ToArray(); // Convert the collection to a string array } }siva nathanRank #1,694 · 899 reputationPosted Aug 7, 2023, 9:16 PM EDT
Hi imran,
Request object as like below
[{
"Name": " siva",
"Dob":" 05-09-2000"
}]
How can I get the count of array from above object
Tahir AnsariRank #366 · 6.8K reputationPosted Aug 8, 2023, 12:30 AM EDT
using System; using System.Collections.Generic; public async List ConvertObjToArray(object obj) { if (obj is IEnumerable stringArray) { List convertedArray = new List(stringArray); int count = convertedArray.Count; return convertedArray; } else { // Handle the case where the object is not an array of strings. // You can throw an exception or return an empty list, depending on your use case. return new List(); } }Amit MohantyRank #112 · 20K reputationPosted Aug 8, 2023, 1:38 AM EDT
using System; using System.Collections.Generic; using Newtonsoft.Json; using Newtonsoft.Json.Linq; class Program { public class Person { public string Name { get; set; } public string Dob { get; set; } } static void Main(string[] args) { string jsonString = @"[ { ""Name"": ""siva"", ""Dob"": ""05-09-2000"" } ]"; List personList = JsonConvert.DeserializeObject>(jsonString); Console.WriteLine("Converted List of Persons:"); foreach (Person person in personList) { Console.WriteLine($"Name: {person.Name}, Dob: {person.Dob}"); } Console.WriteLine($"Number of Persons: {personList.Count}"); } }Cr BhargaviRank #420 · 5.9K reputationPosted Aug 8, 2023, 1:29 AM EDT
Please use the code below which will calculate the count aswell as it will convert obj into array
public async Task> ConvertObjToArray(object obj) { if (obj is IEnumerable stringArray) { List result = stringArray.ToList(); int count = result.Count; return result; } else { // Handle the case where the object cannot be converted to an array of strings. // You might throw an exception or return an empty list based on your requirements. return new List(); } }