[英]Deserialize json object from a combobox data (loaded from database) Winform
Good morning, I'm having a hard time finding solution to my problem.早上好,我很难找到解决问题的方法。 I have a combobox which is populated using datasource which is from DB and those data where JSON objects.
我有一个组合框,它使用来自 DB 的数据源和 JSON 对象的数据填充。 How will I deserialize those data?
我将如何反序列化这些数据? This is the JSON data from Database:
这是来自数据库的 JSON 数据:
{"displayname":{"value":"Test","scope":"contacts","verified":"0"},"address":{"value":"","scope":"private","verified":"0"},"website":{"value":"","scope":"private","verified":"0"},"email":{"value":"","scope":"contacts","verified":"0"},"avatar":{"scope":"contacts","verified":"0"},"phone":{"value":"01234567890","scope":"private","verified":"0"},"twitter":{"value":"","scope":"private","verified":"0"}}
And below is what will be shown in the combobox.下面是将在组合框中显示的内容。 I don't want a JSON object in my combobox but instead a deserialized data.
我不想要组合框中的 JSON 对象,而是反序列化的数据。 Ex: I want to only show the names in my combobox which is the value under displayname in JSON Object.
例如:我只想在我的组合框中显示名称,这是 JSON 对象中 displayname 下的值。
Download and install Newtonsoft.Json
package from NuGet.从 NuGet 下载并安装
Newtonsoft.Json
包。 then you can Deserialize your JSON string and add it to your Combobox items.然后您可以反序列化您的 JSON 字符串并将其添加到您的组合框项目中。
eg例如
//using Newtonsoft.Json;
//using Newtonsoft.Json.Linq;
string jsonStr = "Your JsonString ... ";
var parsed = JsonConvert.DeserializeObject<JObject>(jsonStr);
string[] lst = parsed.Properties().Select(q => q.Name).ToArray();
comboBox1.Items.AddRange(lst);
if you want to have separate DisplayMember
and ValueMember
you can use your JProperty
array as a DataSource.如果您想拥有单独的
DisplayMember
和ValueMember
您可以使用您的JProperty
数组作为数据源。
eg例如
string jsonStr = "Your JsonString ... ";
var parsed = JsonConvert.DeserializeObject<JObject>(jsonStr);
comboBox1.DataSource = parsed.Properties().ToArray();
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "Value";
when you use a DataSource the comboBox1.SelectedItem
also is a JProperty.当您使用 DataSource 时,
comboBox1.SelectedItem
也是一个 JProperty。 so you can use this object to access other data in your json string.因此您可以使用此对象访问 json 字符串中的其他数据。
eg例如
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
// selectedValue is equal to `value` in the json string
var selectedValue = ((JProperty)comboBox1.SelectedItem).Value.Value<string>("value");
MessageBox.Show(selectedValue);
}
Create a class as your Combo box Data Source like this,像这样创建一个类作为组合框数据源,
public class Displayname
{
public string scope;
public string value; // as your display member in combobox
public int verified;
}
public class MyData
{
public string address;
public Displayname displayname;
}
then deserialize your JSON to an object of MyData like this然后像这样将您的 JSON 反序列化为 MyData 的对象
string json = @"{
'displayname': {'value' : 'james@example.com',
'scope': 'contacts'},
'verified': '0' } , 'address':'blabla' ";
var t = JsonConvert.DeserializeObject<MyData>(json);
combobox.datasource = t.displayname; // not sure about syntax of this line,
Then in combobox property set the display member as value (this is the name of the property you have in your json and also you have in Deserialized class which is Displayname).然后在组合框属性中将显示成员设置为值(这是您在 json 中拥有的属性名称,也是您在反序列化类中拥有的属性名称,即 Displayname)。 exactly write the the name of the property that you want.
准确地写出您想要的属性的名称。
by the way, value is not good name for display member and the json string you provided I believe is not right json.顺便说一句, value 不是显示成员的好名字,你提供的 json 字符串我认为不是正确的 json。 good luck
祝你好运
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.