获得的文本如下,我想response.write循环输出部门名称,本人小白,求代码:
{"errcode":0,"errmsg":"ok","department":[{"id":1,"name":"业务部","parentid":0,"order":200},{"id":3,"name":"业务一部","parentid":1,"order":400},{"id":4,"name":"业务二部","parentid":3,"order":200}]}
我自己建的实体
public class JsonModel
{
public int errcode { get; set; }
public string errmsg { get; set; }
public DataClass department { get; set; }
}
public class DataClass
{
public int id { get; set; }
public string name { get; set; }
public int parentid { get; set; }
public int order { get; set; }
}
public class JsonModel
{
public int errcode { get; set; }
public string errmsg { get; set; }
public List<DataClass> department { get; set; }
}
JsonModel model = Newtonsoft.Json.JsonConvert.DeserializeObject<JsonModel>(strJson);
model.department.ForEach(s => Console.WriteLine(s.name));
建一个result model
public class Result
{
public int errcode { get; set; }
public string errmsg { get; set; }
public List<DataClass> department { get; set; }
}
网上找一下Newtonsoft.Json这个类库
方法如下:
var m = new Result();
//do……
string r = Newtonsoft.Json.JsonConvert.SerializeObject(m );
response.Write(r);
response.End();