Json数据
[{'id':1,'name':'北京'},{'id':2,'name':'上海'},{'id':3,'name':'天津'},{'id':4,'name':'重庆'},{'id':5,'name':'香港'},{'id':6,'name':'澳门'},{'id':7,'name':'台湾'},{'id':8,'name':'黑龙江'},{'id':9,'name':'吉林'},{'id':10,'name':'辽宁'},{'id':11,'name':'内蒙古'},{'id':12,'name':'河北'},{'id':13,'name':'河南'},{'id':14,'name':'山西'},{'id':15,'name':'山东'},{'id':16,'name':'江苏'},{'id':17,'name':'浙江'},{'id':18,'name':'福建'},{'id':19,'name':'江西'},{'id':20,'name':'安徽'},{'id':21,'name':'湖北'},{'id':22,'name':'湖南'},{'id':23,'name':'广东'},{'id':24,'name':'广西'},{'id':25,'name':'海南'},{'id':26,'name':'贵州'},{'id':27,'name':'云南'},{'id':28,'name':'四川'},{'id':29,'name':'西藏'},{'id':30,'name':'陕西'},{'id':31,'name':'宁夏'},{'id':32,'name':'甘肃'},{'id':33,'name':'青海'},{'id':34,'name':'新疆'}]
实体
public class counties { public int id { get; set; } public string name { get; set; } }
分割为List并解析值
static void Main(string[] args) { StreamReader sR1 = new StreamReader(@"F:\1\province.txt"); string nextline = sR1.ReadLine(); List<counties> objects = JsonConvert.DeserializeObject<List<counties>>(nextline); foreach (counties c in objects) { Console.WriteLine("name:{0}", c.name); } Console.ReadKey(); }
解析效果