调用第三方接口,返回的json字符串,为了方便使用转为C#匿名对象:

 ///  /// json转为匿名对象 ///  ///  ///  ///  ///  public static T Json2AnonymousType(string json, T anonymousTypeObject) { return JsonConvert.DeserializeAnonymousType(json, anonymousTypeObject); }

收到的用户json信息字符串

 {"uid": "654","organName": "测试学校","organCode": "380001","roles": "[131, 492]","organId": "344e22a61","userType": "1","organType": "500","userName": "测试老师","rkxx": [],"userId": "teacher","login_phone": "1800000000","login_email": "2000@qq.com","class": "2022级4班","grade": "年级" }

转换为匿名对象

var user = Utility.Json2AnonymousType(userJson, new{uid = "", // 用户标识userId = "",// 账户名userName = "",// 用户姓名organName = "", // 学校机构名称userType = "",// 用户类型login_phone="", // 手机号码login_email="", // 邮箱grade="", // 年级名称@class="" // 班级名称});

使用:

 Console.WriteLine(user.uid); Console.WriteLine(user.userName); Console.WriteLine(user.@class); // json中没有的属性获取到为null

注意:对方用户对象中的班级用了C#关键字class,直接使用 class=””是不可以的,前边加“@”符号即可。