简体   繁体   中英

How to change class object into Array List in c# windows form

Product_Details result = response.ResultAs<Product_Details>();
ArrayList myarr = result;
foreach (var item in result.ToString())
{

}

I want to change the variable result into an array list because this variable is containing data of full table and I need to iterate it.

IF i understand correctly, you are saying that the response contains multiple objects of type Product_Details. I do however need some more information; is the response coming in a JSON format (does it need to be serialized for example?).

Either way try something along the lines of this;

         List<Product_Details> result = new List<Product_Details>(); // make a new list
         result = response.ResultAs<List<Product_Details>>(); // assign response to list       
         foreach (Product_Details pd in result)
         {
             // use pd. to access the variable
         }

// (i kept the .ResultAs since i dont know how or what your response object is/has,but did cast it to a list explicitly)

If you need to find out on how to deserialize JSON objects you can start Here

As discussed in the comments you said it was in JSON format, If you are using NewtonSoft.JSON (nuget) the code would be:

 using (client)
                {
                    HttpResponseMessage result = await client.GetAsync(tmpUri);
                    if (result.IsSuccessStatusCode)
                    {
                        var content = await result.Content.ReadAsStringAsync();
                        oc = JsonConvert.DeserializeObject<ObservableCollection<T>>(content);
                      }
                }

(for the sake of clarity where this code should be i included the api call. you can of course, omit this.)

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