C# JSON到Devexpress Winforms LookUpEdit控件
我的JSON文件内容如下:C# JSON到Devexpress Winforms LookUpEdit控件,c#,json,devexpress,devexpress-windows-ui,C#,Json,Devexpress,Devexpress Windows Ui,我的JSON文件内容如下: { 01: "One", 02: "Two", 03: "Three", 04: "Four", 05: "Five", 06: "Six", 07: "Seven", 08: "Eight", 09: "Nine", 10: "Ten" } var json = File.ReadAllText(@"numbers.json"); var array = JObject.Parse(json); lo
{
01: "One",
02: "Two",
03: "Three",
04: "Four",
05: "Five",
06: "Six",
07: "Seven",
08: "Eight",
09: "Nine",
10: "Ten"
}
var json = File.ReadAllText(@"numbers.json");
var array = JObject.Parse(json);
lookUpEdit1.Properties.DropDownRows = array.Count > 10 ? 10 : array.Count;
lookUpEdit1.Properties.DisplayMember = "Key";
lookUpEdit1.Properties.ValueMember = "Value";
lookUpEdit1.Properties.DataSource = array.ToList();
lookUpEdit1.Properties.Columns.Add(new LookUpColumnInfo("Key"));
我使用的是Newtonsoft.Json
库。我试着这样做:
{
01: "One",
02: "Two",
03: "Three",
04: "Four",
05: "Five",
06: "Six",
07: "Seven",
08: "Eight",
09: "Nine",
10: "Ten"
}
var json = File.ReadAllText(@"numbers.json");
var array = JObject.Parse(json);
lookUpEdit1.Properties.DropDownRows = array.Count > 10 ? 10 : array.Count;
lookUpEdit1.Properties.DisplayMember = "Key";
lookUpEdit1.Properties.ValueMember = "Value";
lookUpEdit1.Properties.DataSource = array.ToList();
lookUpEdit1.Properties.Columns.Add(new LookUpColumnInfo("Key"));
它给出如下错误:“JObject”不包含“ToList”的定义,并且找不到接受“JObject”类型的第一个参数的扩展方法“ToList”(是否缺少using指令或程序集引用?
如何从JSON调用Devexpress Winforms LookUpEdit
文件?从中,您需要数据源是实现System.Collections.IList
或System.ComponentModel.IListSource
接口的任何对象。这对如何将JSON对象转换为列表提出了挑战。例如,您将您的键(“01”、“02”和…)称为什么?您确定JSON的格式正确吗(01
而不是“01”
)
以下是如何使用IList接口将JSON对象转换为对象
using System;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
public class Program
{
public static void Main()
{
string json = @"
{
'01':'One',
'02':'Two',
'03':'Three',
'04':'Four',
'05':'Five',
'06':'Six',
'07':'Seven',
'08':'Eight',
'09':'Nine',
'10':'Ten'
}";
JObject o = JObject.Parse(json);
NumberObject zero = new NumberObject("00", "zero");
List<NumberObject> list = new List<NumberObject>();
foreach (JProperty p in o.Properties())
{
NumberObject num = new NumberObject(p.Name, (string)p.Value);
list.Add(num);
}
// now list can be used as your data source as it is of type List<NumberObject>
}
}
public class NumberObject
{
public string Name {get; set;}
public string Value {get; set;}
public NumberObject(string numName, string numValue)
{
Name = numName;
Value = numValue;
}
}
使用系统;
使用System.Collections.Generic;
使用Newtonsoft.Json.Linq;
公共课程
{
公共静态void Main()
{
字符串json=@”
{
‘01’:‘一’,
‘02’:‘二’,
‘03’:‘三’,
'04':'4',
‘05’:‘五’,
‘06’:‘6’,
‘07’:‘七’,
‘08’:‘8’,
‘09’:‘9’,
‘10’:‘10’
}";
JObject o=JObject.Parse(json);
NumberObject零=新的NumberObject(“00”,“零”);
列表=新列表();
foreach(o.Properties()中的JProperty p)
{
NumberObject num=新的NumberObject(p.Name,(string)p.Value);
列表。添加(num);
}
//现在列表可以用作您的数据源,因为它属于列表类型
}
}
公共类数字对象
{
公共字符串名称{get;set;}
公共字符串值{get;set;}
PublicNumberObject(字符串numName、字符串numValue)
{
Name=numName;
值=numValue;
}
}
或者,如果可以稍微修改初始输入,则可以使用此方法:
string json = @"{
'd': [
{
'Name': 'John Smith'
},
{
'Name': 'Mike Smith'
}
]
}";
JObject o = JObject.Parse(json);
JArray a = (JArray)o["d"];
IList<Person> person = a.ToObject<IList<Person>>();
Console.WriteLine(person[0].Name);
// John Smith
Console.WriteLine(person[1].Name);
// Mike Smith
stringjson=@”{
“d”:[
{
“姓名”:“约翰·史密斯”
},
{
“姓名”:“迈克·史密斯”
}
]
}";
JObject o=JObject.Parse(json);
JArray a=(JArray)o[“d”];
IList person=a.ToObject();
Console.WriteLine(人员[0]。姓名);
//约翰·史密斯
Console.WriteLine(person[1].Name);
//迈克·史密斯
使用新的DevExpress框架,它们提供了非常棒的功能,比如您可以通过数据源向导选择JSON对象,因此您无需进行此类编码
如果使用较旧的版本,则可以使用IEnumerable.Select(x=>x.xyz)
features迭代json的所有值,并通过Lookup.properies.items.add()将其放入lookupEdit
谢谢