简体   繁体   中英

C# populate comboBox from JSON online

I need to populate a comboBox from a JSON online. It is a WindowsForms Project, in C#. A PHP page returns the following string:

[{"user_id":"1","first_name":"Joao","last_name":"Silva"},{"user_id":"2","first_name":"Maria","last_name":"Santos"},{"user_id":"3","first_name":"Rosa","last_name":"Costa"}]

user_id would be the comboBox ID and first_name + last_name would be the text. I tried many ways, but no one worked fine. Any suggestion?

One of my attempts:

public class User
        {
            public int user_id { get; set; }
            public string first_name { get; set; }
            public string last_name { get; set; }
        }

        public class LegendsUsers
        {
            public int user_id { get; set; }
            public string first_name { get; set; }
            public string last_name { get; set; }
        }

        public class RootObject
        {
            public List<User> Users { get; set; }
            public List<LegendsUsers> LegendsUsers { get; set; }
        }

        public class ComboboxItem
        {
            public string Text { get; set; }
            public object Value { get; set; }

            public override string ToString()
            {
                return Text;
            }
        }

    String resposta = new
    WebClient().DownloadString("http://www.sample.com/readjson.php");
            var x = JsonConvert.DeserializeObject<RootObject>(resposta);
            foreach (var user in x.Users)
            {
               ComboboxItem item = new ComboboxItem();
               item.Text = user.first_name + " " + user.last_name;
               item.Value = user.user_id;
               comboBox1.Items.Add(item);
            }

Error:

Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON array (eg [1,2,3]) into type 'NB_WBF_Demo.NB_WBF_Demo+RootObject' because the type requires a JSON object (eg {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (eg {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (eg ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. Path '', line 1, position 1.

Since your json is an array/List, not an object, your deserialization code should be something like this

public class RootObject
{
    public string user_id { get; set; }
    public string first_name { get; set; }
    public string last_name { get; set; }
}

var x = JsonConvert.DeserializeObject<List<RootObject>>(resposta);
foreach (var user in x)
{
   .....
}

You will need to deserialize the JSON feed. After you have done this, you can set the properties for ID and Text, and set the data source of the ComboBox.

Look at JSON.NET for the deserialization. This blog post should help you:

http://www.hanselman.com/blog/NuGetPackageOfTheWeek4DeserializingJSONWithJsonNET.aspx

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