Hi
I have below data returned by API. I want to display ID & Name in deopdown list
C#
{"status":true,"data":[{"id":"66","name":"Amazon Shipping"},{"id":"37","name":"Amazon Shipping 1 KG"},{"id":"62","name":"Amazon Shipping 10 KG"},{"id":"52","name":"Amazon Shipping 2 KG"},{"id":"159","name":"Amazon Shipping 20 KG"},{"id":"53","name":"Amazon Shipping 5 KG"},C#
public class Root
{
public bool status { get; set; }
public List data { get; set; }
}
public class Data
{
public string id { get; set; }
public string name { get; set; }
}Thanks

Aravind GovindarajRank #430 · 5.8K reputationPosted Jan 8, 2023, 6:15 AM EST
Hi,
First, deserialize the JSON into Root. So you will get in status and data(list of objects)
Follow this to assign into DDL
Pasang TamangRank #449 · 5.6K reputationPosted Jan 8, 2023, 8:31 AM EST
Hi,
As suggested by Aravind, you have to deserialize the JSON string into Root object. Please check below code
var content = "{\"status\":true,\"data\":[{\"id\":\"66\",\"name\":\"Amazon Shipping\"},{\"id\":\"37\",\"name\":\"Amazon Shipping 1 KG\"},{\"id\":\"62\",\"name\":\"Amazon Shipping 10 KG\"},{\"id\":\"52\",\"name\":\"Amazon Shipping 2 KG\"},{\"id\":\"159\",\"name\":\"Amazon Shipping 20 KG\"},{\"id\":\"53\",\"name\":\"Amazon Shipping 5 KG\"}]}"; var objContent = JsonConvert.DeserializeObject(content); DropDownList dropDown = new DropDownList();// Assuming this is your drop downlist dropDown.DataSource = objContent.data; dropDown.DataBind();Regards,
Pasang
Jignesh KumarRank #91 · 22.9K reputationPosted Jan 8, 2023, 10:53 PM EST
Hello,
You need to combine your value and text into another field and then you can use that to bind dropdownvalues,
var jsonResponse = "{\"status\":true,\"data\":[{\"id\":\"66\",\"name\":\"Amazon Shipping\"},{\"id\":\"37\",\"name\":\"Amazon Shipping 1 KG\"},{\"id\":\"62\",\"name\":\"Amazon Shipping 10 KG\"},{\"id\":\"52\",\"name\":\"Amazon Shipping 2 KG\"},{\"id\":\"159\",\"name\":\"Amazon Shipping 20 KG\"},{\"id\":\"53\",\"name\":\"Amazon Shipping 5 KG\"}]}"; ; var res = JsonConvert.DeserializeObject(jsonResponse.ToString()); var dropDownList = new List(); foreach (var item in res.data) { var DropDownvalues = new DropDownList() { Value = Convert.ToInt32(item.id), Text = item.id + '-' + item.name }; dropDownList.Add(DropDownvalues); }public class Root { public bool status { get; set; } public List data { get; set; } } public class Data { public string id { get; set; } public string name { get; set; } } public class DropDownList { public int Value { get; set; } public string Text { get; set; } }in above class we have created response with value - Text means you can see
66 - Amazone Shipping,
37 - Amazon Shipping 1 KG
you need to use dropDownList to bind your dropdown, you can assing this list to viewBag or you can use this model in your view model to bind dropdown